【LeetCode】236. The nearest common ancestor of the binary tree, JZ36 binary search tree and doubly linked list

 Author: Xiao Lu

 

Column: "Leetcode"

Favorite words: The world is more beautiful because of the youth who stand up. —— "People's Daily"


 236. Nearest Common Ancestor of Binary Tree

236. Nearest Common Ancestor of Binary Tree

Title description:

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

The definition of the nearest common ancestor in Baidu Encyclopedia is: "For two nodes p and q of a rooted tree T, the nearest common ancestor is expressed as a node x, satisfying that x is the ancestor of p and q and the depth of x is as large as possible (a A node can also be its own ancestor)."

Example:

Ideas:

Here we use two stacks to store the paths of p and q, so that the problem can be converted into a linked list intersection problem

Let the big one take the difference step first, and then walk at the same time to find the same node and return

code:

class Solution {
public:
        bool GetPath(TreeNode*root,TreeNode*x,stack<TreeNode*>&Path)
        {
            if(root==nullptr)
            return false;
            Path.push(root);
            if(x==root)
            return true;
            
            if(GetPath(root->left,x,Path))
                return true;
            if(GetPath(root->right,x,Path))
                return true;
            Path.pop();
            return false;

        }

    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        stack<TreeNode*>pPath,qPath;
        GetPath(root,p,pPath);
        GetPath(root,q,qPath);
        while(pPath.size()!=qPath.size())
        {
            if(pPath.size()>qPath.size())
            pPath.pop();
            else
            qPath.pop();
        }
        while(pPath.top()!=qPath.top())
        {
            pPath.pop();
            qPath.pop();
        }
        return pPath.top();
    }
};

 JZ36 binary search tree and doubly linked list

Binary Search Tree and Doubly Linked List Develop Paper

 Title description:

Input a binary search tree and convert the binary search tree into a sorted doubly linked list. As shown below


Requirements: space complexity O(1) (that is, operate on the original tree), time complexity O(n)

Notice:

1. It is required that no new nodes can be created, only the pointing of the node pointers in the tree can be adjusted. After the conversion is completed, the left pointer of the node in the tree needs to point to the predecessor, and the right pointer of the node in the tree needs to point to the successor 2. Return the pointer of the
first node in the linked list
3. The TreeNode returned by the function has left and right pointers, which can be seen in fact into a data structure of a doubly linked list

4. You don't need to output the doubly linked list, the program will automatically print out according to your return value

Example:

Ideas:

Use inorder traversal to change the pointing of nodes

code:

class Solution {
public:
    void _Convert(TreeNode*&prev,TreeNode*cur)
	{
		if(cur==nullptr)
		return ;
		
		_Convert(prev,cur->left);
		//root
			cur->left=prev;
			if(prev)
		    prev->right=cur;
			prev=cur;
		
		_Convert(prev,cur->right);
	} 

    TreeNode* Convert(TreeNode* pRootOfTree) {
        TreeNode*prev=nullptr;
		TreeNode*cur=pRootOfTree;
		
		_Convert(prev,cur);
		TreeNode*head=pRootOfTree;
		while(head&&head->left)
		{
			head=head->left;
		}
		return head;
    }
};

Guess you like

Origin blog.csdn.net/m0_69061857/article/details/131054990