二叉树
Charlesjjc
这个作者很懒,什么都没留下…
展开
-
108. 将有序数组转换为二叉搜索树
108. 将有序数组转换为二叉搜索树class Solution { public TreeNode sortedArrayToBST(int[] nums) { return build(nums,0,nums.length - 1); } public TreeNode build(int[] nums, int l, int r){ if(r < l) return null; int mid = (r + l) /原创 2021-08-04 11:47:07 · 33 阅读 · 0 评论 -
538. 把二叉搜索树转换为累加树
538. 把二叉搜索树转换为累加树class Solution { List<TreeNode> arr; public TreeNode convertBST(TreeNode root) { arr = new ArrayList<>(); dfs(root); for(int i = arr.size() - 2;i >= 0;i--){ arr.get(i).val += ar原创 2021-08-04 02:21:19 · 56 阅读 · 0 评论 -
669. 修剪二叉搜索树
669. 修剪二叉搜索树class Solution { public TreeNode trimBST(TreeNode root, int low, int high) { if(root == null) return null; if(root.val <= high && root.val >= low) { root.left = trimBST(root.left,low,high);原创 2021-08-04 01:31:21 · 91 阅读 · 0 评论 -
450. 删除二叉搜索树中的节点
450. 删除二叉搜索树中的节点class Solution { public TreeNode deleteNode(TreeNode root, int key) { if(root == null) return null; if(root.val > key) root.left = deleteNode(root.left,key); else if (root.val < key) root.right = deleteN原创 2021-08-03 23:44:20 · 42 阅读 · 0 评论 -
701. 二叉搜索树中的插入操作
701. 二叉搜索树中的插入操作class Solution { public TreeNode insertIntoBST(TreeNode root, int val) { if(root == null) return new TreeNode(val); if(root.val > val) root.left = insertIntoBST(root.left, val); if(root.val < val) root.r原创 2021-08-03 22:04:18 · 44 阅读 · 0 评论 -
235. 二叉搜索树的最近公共祖先
235. 二叉搜索树的最近公共祖先class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { List<TreeNode> arr = new ArrayList<>(); build(root,arr); int max = Math.max(p.val,q.val); int原创 2021-08-03 17:30:21 · 38 阅读 · 0 评论 -
236. 二叉树的最近公共祖先
236. 二叉树的最近公共祖先class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root == null) return null; List<TreeNode> arr1 = new ArrayList<>(); List<TreeNode> arr2 = new原创 2021-08-03 17:29:20 · 49 阅读 · 0 评论 -
501. 二叉搜索树中的众数
501. 二叉搜索树中的众数class Solution { int max = 0; ArrayList<Integer> ans = new ArrayList<>(); int curmax = 1; TreeNode pre; TreeNode cur; public int[] findMode(TreeNode root) { dfs(root); int[] res = new int原创 2021-08-03 02:04:30 · 47 阅读 · 0 评论 -
530. 二叉搜索树的最小绝对差
530. 二叉搜索树的最小绝对差class Solution { public int getMinimumDifference(TreeNode root) { int leftMin = Integer.MAX_VALUE; int rightMin = Integer.MAX_VALUE; int minl = Integer.MAX_VALUE; int minr = Integer.MAX_VALUE;原创 2021-08-02 23:59:07 · 59 阅读 · 0 评论 -
98. 验证二叉搜索树
98. 验证二叉搜索树class Solution { public boolean isValidBST(TreeNode root) { boolean l = true; boolean r = true; TreeNode nodeLeft = root.left; TreeNode nodeRight = root.right; if(root == null) return true;原创 2021-08-02 22:49:58 · 40 阅读 · 0 评论 -
700. 二叉搜索树中的搜索
700. 二叉搜索树中的搜索class Solution { public TreeNode searchBST(TreeNode root, int val) { if(root.val == val) return root; if(root.val < val && root.right != null) return searchBST(root.right, val); if(root.val > val &原创 2021-08-02 21:37:32 · 43 阅读 · 0 评论 -
617. 合并二叉树
617. 合并二叉树class Solution { public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { TreeNode node = new TreeNode(); if(root1 == null && root2 == null) return null; if(root1 == null || root2 == null){ i原创 2021-08-02 21:30:38 · 33 阅读 · 0 评论 -
654. 最大二叉树
654. 最大二叉树DFSclass Solution { int[] arr; HashMap<Integer,Integer> map = new HashMap<>(); public TreeNode constructMaximumBinaryTree(int[] nums) { arr = nums.clone(); for(int i = 0; i < nums.length;i+原创 2021-08-02 21:16:57 · 43 阅读 · 0 评论 -
106. 从中序与后序遍历序列构造二叉树
106. 从中序与后序遍历序列构造二叉树class Solution { HashMap<Integer,Integer> in_map = new HashMap<>(); int[] post; public TreeNode buildTree(int[] inorder, int[] postorder) { for(int i = 0;i < inorder.length;i++) in_map.put(inorder[i原创 2021-07-31 13:04:27 · 52 阅读 · 0 评论 -
112. 路径总和
112. 路径总和class Solution { public boolean hasPathSum(TreeNode root, int targetSum) { if(root == null) return false; return dfs(root,targetSum,0) == 1 ? true: false; } public int dfs(TreeNode node,int targetSum,int num){原创 2021-07-31 02:38:02 · 92 阅读 · 0 评论 -
513. 找树左下角的值
513. 找树左下角的值class Solution { int maxdep = -1; int max = 0; public int findBottomLeftValue(TreeNode root) { dfs(root,0); return max; } public void dfs(TreeNode root,int depth){ if(root == null) return;原创 2021-07-31 01:29:30 · 37 阅读 · 0 评论 -
404. 左叶子之和
404. 左叶子之和class Solution { public int sumOfLeftLeaves(TreeNode root) { Queue<TreeNode> queue = new LinkedList<>(); int sum = 0; if(root == null) return sum; queue.add(root); while(!queue.isEmpty())原创 2021-07-31 00:30:03 · 40 阅读 · 0 评论 -
257. 二叉树的所有路径
257. 二叉树的所有路径DFS方法class Solution { public List<String> binaryTreePaths(TreeNode root) { List<String> res = new ArrayList<String>(); dfs(root,res,""); return res; } public void dfs(TreeNode node,Lis原创 2021-07-30 22:59:59 · 58 阅读 · 0 评论 -
110. 平衡二叉树
110. 平衡二叉树class Solution { int flag = 1; public boolean isBalanced(TreeNode root) { getDepth(root); return flag == 1? true:false; } public int getDepth(TreeNode node){ if(node == null) return 0; .原创 2021-07-30 19:50:11 · 40 阅读 · 0 评论 -
222. 完全二叉树的节点个数
222. 完全二叉树的节点个数DFSclass Solution { public int countNodes(TreeNode root) { if(root == null) return 0; return 1 + countNodes(root.left) + countNodes(root.right); }}O(n)的遍历方法,没有用到完全二叉树的性质DFS变种class Solution { public原创 2021-07-30 19:09:04 · 43 阅读 · 0 评论 -
111. 二叉树的最小深度
111. 二叉树的最小深度DFSclass Solution { public int minDepth(TreeNode root) { if(root == null) return 0; int left_d = minDepth(root.left); int right_d = minDepth(root.right); if(root.left == null && root.right != nu原创 2021-07-30 17:46:11 · 39 阅读 · 0 评论 -
559. N 叉树的最大深度
559. N 叉树的最大深度class Solution { public int maxDepth(Node root) { if(root == null) return 0; int res = 0; for(Node i:root.children){ res = Math.max(maxDepth(i),res); } return res + 1; }}...原创 2021-07-29 01:47:01 · 30 阅读 · 0 评论 -
104. 二叉树的最大深度
104. 二叉树的最大深度class Solution { public int maxDepth(TreeNode root) { if(root == null) return 0; int left_h = maxDepth(root.left); int right_h = maxDepth(root.right); return Math.max(left_h,right_h) + 1; }}原创 2021-07-29 01:20:51 · 32 阅读 · 0 评论 -
leetcode101.对称二叉树
101. 对称二叉树这题算简单???我觉得中等不过分迭代法,BFS层序遍历class Solution { public boolean isSymmetric(TreeNode root) { Queue<TreeNode> queue = new LinkedList<>(); //空树或者只有一个root if(root == null || (root.left == null && ro原创 2021-07-28 21:51:30 · 67 阅读 · 0 评论 -
leetcode226. 翻转二叉树
226. 翻转二叉树DFS:递归class Solution { public TreeNode invertTree(TreeNode root) { if(root == null) return root; TreeNode tmp = root.left; root.left = root.right; root.right = tmp; invertTree(root.left);原创 2021-07-28 13:58:56 · 56 阅读 · 0 评论 -
leetcode层序遍历8道题打包带走(BFS专项训练)
这题用递归做https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/class Solution { List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> levelOrderBottom(TreeNode root) { checkf原创 2021-07-27 19:13:20 · 81 阅读 · 0 评论 -
leetcode102.层序遍历(DFS)
https://leetcode-cn.com/problems/binary-tree-level-order-traversal/class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new LinkedList<>(); List<TreeNode&原创 2021-07-27 17:49:25 · 293 阅读 · 0 评论 -
二叉树的前中后序遍历
先用递归法写一下二叉树的遍历,再用迭代法。递归法:class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); preorder(root, res); return res; } //前序遍历 public v原创 2021-07-27 01:04:43 · 43 阅读 · 0 评论