Data structures and algorithms (1) Spur task 4--lowest Common Ancestor of a Binary Tree

Source: Internet
Author: User

The title is as follows: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/

Given a binary tree, find the lowest common ancestor (LCA) of the Given nodes in the tree.

According to the definition of the LCA in Wikipedia: "The lowest common ancestor is defined between," nodes V and W as the L Owest node in T, have 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   4

For example, the lowest common ancestor (LCA) of nodes and are 5 1 3 . Another example is LCA of nodes 5 4 5 and are, since a node can be a descendant of itself according to the L CA definition.

Since the topic has given the data structure and function (see the Code section below), it is not pleasant to use pointers pointing to both parents, but the common ancestor looking for p and Q two nodes seems to be looking again from the next direction. So we can consider recursion: the first child to be found is the node of the P or Q ancestor, or the node of the ancestor of Q, which is p. If these two points are met, it is good to return directly; if it is not an ancestor, ignore it; if it is an ancestor, it can be returned by recursion, and it is possible to find it from the bottom up.

The code is as follows:

/** Definition for a binary tree node. * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * TreeNode (int x): Val (x), left (null), right (NULL) {} *}; */classSolution { Public: TreeNode* Lowestcommonancestor (treenode* root, treenode* p, treenode*q) {if(Root = =NULL) {        returnNULL;//to the bottom, nothing, return empty    }    if(root = = P | | root = =q) {returnRoot//a node was found and returned to the node .    }    //RecursiveTreeNode *left = Lowestcommonancestor (root->Left , p, q); TreeNode*right = Lowestcommonancestor (root->Right , p, q); if(Left &&Right ) {        returnRoot//to find the node ancestors are left and right children, the node is the result    }    Else if(left) {returnLeft//just an ancestor that will "upload" the result    }    returnRight ; }};

There's no time. Ah ah ah ah oh ah ah ah oh ah ah ah

This is not written until 00:00 November 23, 2015.

However, in order to maintain the style of unification, or to add a few think well-written blog

Report:

http://arsenal591.blog.163.com/blog/static/253901269201510169448656

Http://www.cnblogs.com/ocNflag/p/4967695.html

Http://blog.sina.com.cn/s/blog_1495db3970102w4wl.html

Http://www.cnblogs.com/fighter-MaZijun/p/4979318.html

Http://www.cnblogs.com/lqf-96/p/lowest-common-ancestor-of-a-binary-tree.html

Re-attached://There is a picture that seems to be more intuitive than the one I wrote above

Http://www.cnblogs.com/Jueis-lishuang/p/4984971.html

The last of the last:

Originally here to pretend to have some literary talent, write some now only think half of the things, but write not to me personally seems to have little meaning. This week seems a little busy, too.

Sigh two sentences just fine:

It seems that the great God is not the same creature as the salted fish

Never mind...... That's enough, too.

Data structures and algorithms (1) Spur task 4--lowest Common Ancestor of a Binary Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.