LeetCode - Binary Tree Articles (8)

The order and ideas of brushing questions come from the code caprice record, website address: https://programmercarl.com

Table of contents

236. Nearest Common Ancestor of Binary Tree 

235. Nearest Common Ancestor of Binary Search Tree

         iteration

recursion

701. Insertion in a Binary Search Tree

450. Delete a Node in a Binary Search Tree

236. Nearest Common Ancestor of Binary Tree

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 )."

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 *(思路:对于一个结点,只要其左子树出现p或q,或右子树出现p或q,那么该节点就是节点p和q的最近公共                    
    祖先;
 *如果递归遍历遇到q,就将q返回,遇到p就将p返回,那么如果左右子树的返回值都不为空,说明此时的中节 
    点,一定是q和p的最近祖先。
 *
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        //递归结束条件
		if(root==p||root==q||root==null){
			return root;
		}

		//左
		TreeNode left=lowestCommonAncestor(root.left,p,q);
		//右
		TreeNode right=lowestCommonAncestor(root.right,p,q);

		//中
		if(left!=null&&right!=null){
			return  root;  //最近公共祖先
		}else if(left==null&&right!=null){
			return right;   // 若找到一个节点  继续向上返回直到根节点
		} else if (left != null && right == null) {
			return left;   // 若找到一个节点  继续向上返回直到根节点
		}else {
			return null;   //没找到结点
		}
    }
}

235. Nearest Common Ancestor of Binary Search Tree

Given a binary search 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 node can also be its own ancestor )."

 iteration

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
			//迭代
        while (root!=null){
			if(root.val>=p.val&&root.val<=q.val||root.val<=p.val&&root.val>=q.val||root==null){
				return root;
			}
			if(root.val>p.val&&root.val>q.val){
				root=root.left;
			}else if(root.val<p.val&&root.val<q.val){
				root=root.right;
			}
		}
		return root;
    }
}

recursion

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
			//递归
	    if(root==null){
			return null;
		}
		if(root.val>p.val&&root.val>q.val){
			TreeNode left=lowestCommonAncestor1(root.left,p,q);
			if(left!=null){
				return left;
			}
		}
		if(root.val<p.val&&root.val<q.val){
			TreeNode right=lowestCommonAncestor1(root.right,p,q);
			if(right!=null){
				return right;
			}
		}
		return root;
    }
}

701. Insertion in a Binary Search Tree

Given a binary search tree (BST) root node  root and a value to insert into the tree  value , insert a value into the binary search tree. Returns the root node of the binary search tree after insertion. The input data  guarantees  that the new value is different from any node value in the original binary search tree.

Note that there may be more than one efficient way of inserting, as long as the tree remains a binary search tree after inserting. You can return  any valid result  .

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 * (思路:其实可以不考虑题目中提示所说的改变树的结构的插入方式。
 * 只要按照二叉搜索树的规则去遍历,遇到空节点就插入节点就可以了。
 */
class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        //递归终止条件,当遍历到空节点时,就是要插入节点的时候,返回要插入的节点
		if(root==null){
			TreeNode node=new TreeNode(val);
			return node;
		}
		if(root.val<val){
			root.right=insertIntoBST(root.right,val);
		}
		if(root.val>val){
			root.left=insertIntoBST(root.left,val);
		}
		return  root;
    }
}

450. Delete a Node in a Binary Search Tree

Given a root node root  of a binary search tree  and a value  key , delete the node corresponding to the key  in the binary search tree  , and ensure that the nature of the binary search tree remains unchanged. Returns a reference to the (possibly updated) root node of the binary search tree.

In general, deleting a node can be divided into two steps:

  1. First find the node that needs to be deleted;
  2. If found, delete it.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 * (思路:删除二叉树中节点可以分为以下几种情况:
 * 		1.未找到要删除的节点
 * 		2.找到要删除的节点:
 * 			2.1	删除节点为叶子结点---直接删除
 * 			2.2 删除节点不是叶子结点,但其左孩子为空,右孩子不为空---直接让其父节点指向该节点的右孩子
 * 			2.3	删除节点不是叶子结点,但其右孩子为空,左孩子不为空---直接让其父节点指向该节点的左孩子
 * 			2.4 删除节点不是叶子结点,且左右孩子均不为空:
 * 				右孩子继位,将删除节点的左子树头结点(左孩子)放到删除节点的右子树的最左面节点的左孩子上

 */
class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        //递归终止条件:遇到空直接返回(没找到要删除的节点
		if(root==null){
			return null;
		}
		//找到要删除的节点:返回删除后的根节点
		if(root.val==key){
			//1.删除节点为叶子结点
			if(root.left==null&&root.right==null){
				return null;
			} else if (root.left!=null&&root.right==null) { //2.删除节点左孩子不为空
				return root.left;
			} else if (root.right != null&&root.left==null) { //3.删除节点右孩子不为空
				return root.right;
			}else{  //4.删除节点左右孩子均不为空
				TreeNode node=root.right;
				while(node.left!=null){
					node=node.left;    //找到右子树的最左边的节点
				}
				node.left=root.left;   //把要删除的节点(root)左子树放在cur的左孩子的位置
				return root.right;
			}
		}
	if(root.val>key){
			root.left=deleteNode(root.left, key);
		}
		if(root.val<key){
			root.right=deleteNode(root.right,key);
		}
		return root;
    }
}

Guess you like

Origin blog.csdn.net/zssxcj/article/details/132449308