当前位置:网站首页>07. Advanced application of binary tree
07. Advanced application of binary tree
2022-07-19 16:33:00 【Java baa】
Advanced application of binary tree
One 、 Advanced application of binary tree
1. The binary tree traverses by layer and collects nodes
Leetcode The original title is https://leetcode.cn/problems/binary-tree-level-order-traversal-ii
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
// Test link :https://leetcode.cn/problems/binary-tree-level-order-traversal-ii
public class BinaryTreeLevelOrderTraversalII {
public static class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
TreeNode(int val) {
this.val = val;
}
}
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> ans = new LinkedList<>();
if (root == null) {
return ans;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> curAns = new LinkedList<>();
for (int i = 0; i < size; i++) {
TreeNode curNode = queue.poll();
curAns.add(curNode.val);
if (curNode.left != null) {
queue.add(curNode.left);
}
if (curNode.right != null) {
queue.add(curNode.right);
}
}
ans.add(0, curAns);
}
return ans;
}
}
2. Determine if it's a balanced binary tree
Leetcode The original title is https://leetcode.cn/problems/balanced-binary-tree
// Test link :https://leetcode.cn/problems/balanced-binary-tree
public class BalancedBinaryTree {
public static class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
TreeNode(int val) {
this.val = val;
}
}
public static class Info {
public boolean isBalanced;
public int height;
public Info(boolean i, int h) {
isBalanced = i;
height = h;
}
}
public static boolean isBalanced(TreeNode root) {
return process(root).isBalanced;
}
public static Info process(TreeNode root) {
if (root == null) {
return new Info(true, 0);
}
Info leftInfo = process(root.left);
Info rightInfo = process(root.right);
int height = Math.max(leftInfo.height, rightInfo.height) + 1;
boolean isBalanced = leftInfo.isBalanced && rightInfo.isBalanced
&& Math.abs(leftInfo.height - rightInfo.height) < 2;
return new Info(isBalanced, height);
}
}
3. Whether the path and
Leetcode The original title is https://leetcode.cn/problems/path-sum
// Test link :https://leetcode.cn/problems/path-sum
public class Code03_PathSum {
public static class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
TreeNode(int val) {
this.val = val;
}
}
public static boolean isSum = false;
public static boolean hasPathSum(TreeNode root, int sum) {
if (root == null) {
return false;
}
isSum = false;
process(root, 0, sum);
return isSum;
}
public static void process(TreeNode x, int preSum, int sum) {
if (x.left == null && x.right == null) {
if (x.val + preSum == sum) {
isSum = true;
}
return;
}
// x Is a non leaf node
preSum += x.val;
if (x.left != null) {
process(x.left, preSum, sum);
}
if (x.right != null) {
process(x.right, preSum, sum);
}
}
public static boolean hasPathSum2(TreeNode root, int sum) {
if (root == null) {
return false;
}
return process(root, sum);
}
public static boolean process(TreeNode root, int rest) {
if (root.left == null && root.right == null) {
return root.val == rest;
}
boolean ans = root.left != null && process(root.left, rest - root.val);
ans |= root.right != null && process(root.right, rest - root.val);
return ans;
}
}
4. Collect compliance paths and
Leetcode The original title is https://leetcode.cn/problems/path-sum-ii
import java.util.ArrayList;
import java.util.List;
// Test link :https://leetcode.cn/problems/path-sum-ii
public class PathSumII {
public static class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
TreeNode(int val) {
this.val = val;
}
}
public List<List<Integer>> pathSum(TreeNode head, int targetSum) {
List<List<Integer>> lists = new ArrayList<>();
if (head == null) return lists;
process(head, targetSum, new ArrayList<>(), lists);
return lists;
}
public void process(TreeNode head, int target, List<Integer> list, List<List<Integer>> lists) {
if (head.left == null && head.right == null) {
if (target == head.val) {
list.add(head.val);
ArrayList<Integer> temp = new ArrayList<>(list);
lists.add(temp);
list.remove(list.size() - 1);
}
return;
}
list.add(head.val);
if (head.left != null) {
process(head.left, target - head.val, list, lists);
}
if (head.right != null) {
process(head.right, target - head.val, list, lists);
}
list.remove(list.size() - 1);
}
}
5. Determine if it's a search Binary Tree
Leetcode The original title is https://leetcode.cn/problems/validate-binary-search-tree/submissions/
// Test link :https://leetcode.cn/problems/validate-binary-search-tree/submissions/
public class IsBinarySearchTree {
public static class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
TreeNode(int val) {
this.val = val;
}
}
public static class Info {
public boolean isBST;
public int max;
public int min;
public Info(boolean is, int ma, int mi) {
isBST = is;
max = ma;
min = mi;
}
}
public static Info process(TreeNode x) {
if (x == null) {
return null;
}
Info leftInfo = process(x.left);
Info rightInfo = process(x.right);
int max = x.val;
int min = x.val;
if (leftInfo != null) {
max = Math.max(leftInfo.max, max);
min = Math.min(leftInfo.min, min);
}
if (rightInfo != null) {
max = Math.max(rightInfo.max, max);
min = Math.min(rightInfo.min, min);
}
boolean isValidBST = (leftInfo == null || leftInfo.isValidBST) && (rightInfo == null || rightInfo.isValidBST);
if (!isValidBST) {
return new Info(isValidBST, max, min);
}
isValidBST = (leftInfo == null || leftInfo.max < head.val) && (rightInfo == null || rightInfo.min > head.val);
return new Info(isBST, max, min);
}
}
Two 、 Written in the back
Welcome to your attention , Will often record some problems encountered in algorithm learning .
Please feel free to leave a message to discuss , Let me share with you , Know all but answer !
边栏推荐
- Is it safe to open a fund account online? College students seek guidance
- The read image of OpenCV notes is de distorted and saved as a new image
- Wild pointer problem: review orange Technology
- MySQL regular expression
- 【VScode输出为乱码】解决方法
- Basic usage of numpy in data processing
- Uniapp authorized login to obtain user information and code
- Leetcode 565 array nesting [dfs memory] the way of leetcode in heroding
- Thesis reading_ Medical NLP_ SMedBERT
- Deep learning environment configuration pytoch
猜你喜欢

软件测试培训不靠谱?花费3W学软件测试半年没找到工作?

JMeter 21 天打开 day11

MySQL - unique key constraint for table fields

信创环境下达梦数据库唯一索引异常无法拦截DuplicateKeyException

Cause analysis of the red light of the server and soft switch status on the Vos client

想自学软件测试?无情嘲讽。

Ch549/ch548 learning notes 8 - USB device interrupt processing

Leetcode 300: longest increasing subsequence

QA机器人第二节——召回

Deep learning environment configuration tensorflow2+keras
随机推荐
How to implement Mysql to insert if it does not exist and update if it exists
奥扬科技IPO被终止注册:年营收8亿 苏伟持有67.5%股权
["code" power is fully open, and "chapter" shows strength] list of contributors to the task challenge in the first quarter of 2022
CH549/CH548學習筆記9 - USB Device端點處理過程
同花顺手机软件炒股线上开户要钱吗?开户安全吗?
Leetcode 300: longest increasing subsequence
Does flush mobile phone software need money to open an account online for stock speculation? Is it safe to open an account?
野指针问题: 复习橙其技术
Ch549/ch548 learning notes 8 - USB device interrupt processing
LeetCode 565 数组嵌套[dfs 记忆化] HERODING的LeetCode之路
梅科尔工作室-DjangoWeb 应用框架+MySQL数据库第四次培训
同花顺软件炒股线上开户可以免费吗?开户安全吗?
MySQL 正則錶達式
Ch549/ch548 learning notes 3 - UART
思必驰冲刺科创板:年营收3亿亏3.4亿 阿里与联想之星是股东
JMeter 21 天打卡 day10
[foundation of deep learning] how to understand the channel in convolutional neural network
Chapter 4: emerging, class instantiation strategy with constructor based on cglib
Leetcode 565 array nesting [dfs memory] the way of leetcode in heroding
CSGO突然返回桌面,并且其他应用无反应,如何不重启关闭