236. Lowest Common Ancestor of a Binary Tree
来源:互联网 发布:windows ce 触摸屏 编辑:程序博客网 时间:2024/06/07 14:42
/*Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).” _______3______ / \ ___5__ ___1__ / \ / \ 6 _2 0 8 / \ 7 4For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.思路1:1:利用二个栈来存储两个节点的路径2:使路径长的节点,从栈顶pop节点,直到两个栈的长度相同3:开始从栈顶比较,如果相等,即为最低的父节点思路2: 两个节点在同一个子树或不同子树,在同一个子树直接返回遇到的第一个节点即可; 在不同的子树,当两个节点都遇到后,下一个父节点即是所求*//** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(!root) return NULL; stack<TreeNode*> sp,sq; findNodePath(root,q,sq); findNodePath(root,p,sp); while(!sq.empty() && !sp.empty()){ if(sq.size()>sp.size()) sq.pop(); else if(sq.size()<sp.size()) sp.pop(); else break; } while(!sq.empty() && !sp.empty()){ if(sq.top()==sp.top()) return sq.top(); sq.pop(); sp.pop(); } return NULL; } void findNodePath(TreeNode* root,TreeNode* target,stack<TreeNode*> &st){ if(!root) return; st.push(root); if(st.top()==target) return; if(root->left) findNodePath(root->left,target,st); if(st.top()!=target && root->right) findNodePath(root->right,target,st); if(st.top()!=target) st.pop(); } TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(!root || root==p || root==q) return root; TreeNode* left=lowestCommonAncestor(root->left,p,q); TreeNode* right=lowestCommonAncestor(root->right,p,q); return !left ? right : !right ? left : root; }};
阅读全文
0 0
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree(***)
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- Lowest Common Ancestor of a Binary Tree
- [agc001e]BBQ hard
- JVM_1_运行时内存区域
- 论文:Banerjee A, Ghosh J. On Scaling Up Balanced Clustering Algorithms.[C]笔记
- SSH和SSM流程图
- 主元素 II
- 236. Lowest Common Ancestor of a Binary Tree
- Http协议——Hyper Text Transfer Protocol(超文本传输协议)
- HTML模板引擎-freemarker入门
- WordPress更改“固定链接”后 页面404原因及解决方法(Nginx版)
- 全排列推演+代码
- 排列组合问题集合
- [MYSQL -7]数据过滤
- 【思维】HDU4104Discount【最小子集和】
- 如何生成 .plist文件,plist文件制作教程