Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse the nearest common ancestor of python binary tree

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces how to analyze the recent common ancestors of python binary tree, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

Given a binary tree, find the nearest common ancestor of the two specified nodes in the tree.

The recent common ancestor in Baidu encyclopedia is defined as: "for the two nodes p and Q with root tree T, the nearest common ancestor is expressed as a node x, satisfying that x is the ancestor of p and Q and that x is as deep as possible (a node can also be its own ancestor)."

For example, a binary tree is given as follows: root = [3pd5, 1, 6, 2, 0, 0, 8, null, and 7]

Example 1:

Input: root = [3, 5, 1, 6, 2, 0, 8, 7, 4], p = 5, Q = 1

Output: 3

Explanation: the nearest common ancestor of node 5 and node 1 is node 3.

Example 2:

Input: root = [3, 5, 1, 6, 2, 0, 8, 7, 4], p = 5, Q = 4

Output: 5

Explanation: the nearest common ancestor of node 5 and node 4 is node 5. Because by definition the nearest common ancestor node can be the node itself.

Description:

The values of all nodes are unique.

P and Q are different nodes and all exist in a given binary tree.

Solution 1:

1, if the two nodes are in the left and right subtree, return the current node

2, if it is all in the left subtree, the recursive left subtree

3, if it is all in the right subtree, recursive right subtree

Code implementation

/ * Definition for a binary tree node. * type TreeNode struct {* Val int * Left * TreeNode * Right * TreeNode * * / func lowestCommonAncestor (root * TreeNode, p * TreeNode, Q * TreeNode) * TreeNode {if rootworthy roomnil & & contain (root.Left,p) & & contain (root.Left,q) {return lowestCommonAncestor (root.Left,p,q)} if rootworthy roomnil & & contain (root.Right,p) & & contain (root.Right) Q) {return lowestCommonAncestor (root.Right,p,q)} return root} func contain (root * TreeNode, p * TreeNode) bool {if root==nil {return false} if root.Val==p.Val {return true} return contain (root.Left,p) | | contain (root.Right,p)}

Solution 2:

1, find the path from the root node to the two points

2. Remove the coincident part.

Code implementation

Func lowestCommonAncestor (root * TreeNode, p * TreeNode, Q * TreeNode) * TreeNode {var lrect r [] * TreeNode _, l=findPath (root,p) _, r=findPath (root,q) l=append ([] * TreeNode {root}, l...) R=append ([] * TreeNode {root}, r...) / / for iRO root

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 212

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report