LeetCode - Binary Tree (3)

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

 For the definition and creation of a binary tree, see:

LeetCode ACM Mode - Binary Tree Articles (1)

Table of contents

116. Populate each node's next right node pointer

117. Filling each node's next right node pointer II

 104. Maximum depth of binary tree

 111. Minimum depth of binary tree

  226. Flip Binary Tree

116. Populate each node's next right node pointer

Given a  perfect binary tree  , all its leaf nodes are at the same level, and each parent node has two child nodes. A binary tree is defined as follows:

struct Node {
  int val;
  Node *left;
  Node *right;
  Node *next;
}

Fill each of its next pointers to point to its next right node. If the next right node cannot be found, the next pointer is set to  NULL.

In the initial state, all next pointers are set to  NULL.

/*
// Definition for a Node.
class Node {
    public int val;
    public Node left;
    public Node right;
    public Node next;

    public Node() {}
    
    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, Node _left, Node _right, Node _next) {
        val = _val;
        left = _left;
        right = _right;
        next = _next;
    }
};
*/

class Solution {
    public Node connect(Node root) {
      Deque<Node> que=new ArrayDeque<>();
		if(root==null){
			return null;
		}
		que.offer(root);
		while(!que.isEmpty()){
			int size=que.size();
			Node preNode=null;
			Node node=null;
			for (int i = 0; i < size; i++) {
				if(i==0){
					//取出本层头部结点
					preNode=que.poll();
					node=preNode;
				}else{
					node=que.poll();
					preNode.next=node;
					preNode=node;
				}
				if(node.left!=null){
					que.offer(node.left);
				}
				if(node.right!=null){
					que.offer(node.right);
				}
			}
			preNode.next=null; //本层最后一个节点指向null
		}
		return root;
    }
}


117. Filling each node's next right node pointer II

Given a binary tree:

struct Node {
  int val;
  Node *left;
  Node *right;
  Node *next;
}

Fill each of its next pointers to point to its next right node. If the next right node cannot be found, the next pointer is set to  NULL .

In the initial state, all next pointers are set to  NULL .

/*
// Definition for a Node.
class Node {
    public int val;
    public Node left;
    public Node right;
    public Node next;

    public Node() {}
    
    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, Node _left, Node _right, Node _next) {
        val = _val;
        left = _left;
        right = _right;
        next = _next;
    }
};
*/

class Solution {
    public Node connect(Node root) {
        Deque<Node> que=new ArrayDeque<>();
		if(root==null){
			return null;
		}
		que.offer(root);
		while(!que.isEmpty()){
			int size=que.size();
			Node preNode=null;
			Node node=null;
			for (int i = 0; i < size; i++) {
				if(i==0){
					//取出本层头部结点
					preNode=que.poll();
					node=preNode;
				}else{
					node=que.poll();
					preNode.next=node;
					preNode=node;
				}
				if(node.left!=null){
					que.offer(node.left);
				}
				if(node.right!=null){
					que.offer(node.right);
				}
			}
			preNode.next=null; //本层最后一个节点指向null
		}
		return root;
    }
}

 104. Maximum depth of binary tree

Given a binary tree  root , return its maximum depth.

The maximum depth of a binary tree   is the number of nodes on the longest path from the root node to the furthest leaf node.

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author light
 * @Description 二叉树的最大深度
 *
 * 给定一个二叉树 root ,返回其最大深度。
 * 二叉树的最大深度是指从根节点到最远叶子节点的最长路径上的节点数。
 * @create 2023-08-16 16:46
 */
public class MaxDepthTest {
	public static void main(String[] args) {
		Integer[] arr={3,9,20,null,null,15,7};
		BinaryTree2 tree2=new BinaryTree2(arr); //按数组方式创建二叉树
		System.out.println(maxDepth(tree2.root));
	}
	public static int maxDepth(TreeNode root) {
		Deque<TreeNode> que=new ArrayDeque<>();
		if(root!=null){
			que.offer(root);
		}
		int depth=0;
		while(!que.isEmpty()){
			int size=que.size();
			while(size>0){
				TreeNode node=que.poll();
				if(node.left!=null){
					que.offer(node.left);
				}
				if(node.right!=null){
					que.offer(node.right);
				}
				size--;
				if(size==0){
					depth++;
				}
			}
		}
		return depth;
	}
}

 111. Minimum depth of binary tree

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes on the shortest path from the root node to the nearest leaf node.

Note: A leaf node refers to a node that has no child nodes.

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author light
 * @Description 二叉树的最小深度
 *
 * 给定一个二叉树,找出其最小深度。
 * 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
 * 说明:叶子节点是指没有子节点的节点。
 * @create 2023-08-16 16:58
 */
public class MinDepthTest {
	public static void main(String[] args) {
		Integer[] arr={3,9,20,null,null,15,7};
		BinaryTree2 tree2=new BinaryTree2(arr); //按数组方式创建二叉树
		System.out.println(minDepth(tree2.root));
	}
	public static int minDepth(TreeNode root) {
		Deque<TreeNode> que=new ArrayDeque<>();
		int depth=0;
		if(root!=null){
			que.offer(root);
			depth++;
		}
		while(!que.isEmpty()){
			int size=que.size();
			while(size>0){
				TreeNode node=que.poll();
				if(node.left==null&&node.right==null){
					return depth;
				}
				if(node.left!=null){
					que.offer(node.left);
				}
				if(node.right!=null){
					que.offer(node.right);
				}
				size--;
				if(size==0){
					depth++;
				}
			}
		}
		return depth;

	}
}

  226. Flip Binary Tree

Given the root of a binary tree  root , flip the tree and return its root.

 

/**
 * 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 invertTree(TreeNode root) {
      Deque<TreeNode> que=new ArrayDeque<>();
			if(root!=null){
				que.offer(root);
			}
			while(!que.isEmpty()){
				int size=que.size();
				while(size>0){
					TreeNode node=que.poll();
					swap(node);
					if(node.left!=null){
						que.offer(node.left);
					}
					if(node.right!=null){
						que.offer(node.right);
					}
					size--;
				}
			}
			return root;
		}
		private  void swap(TreeNode root) {
		TreeNode temp=root.left;
		root.left=root.right;
		root.right=temp;
	}

}

Guess you like

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