publicclassBinaryTreePreorderTraversal{ publicstaticvoidmain(String[] args){ Solution s = new BinaryTreePreorderTraversal().new Solution(); }
// Given the root of a binary tree, return the preorder traversal of its nodes' values. // // Example 1: // Input: root = [1,null,2,3] // Output: [1,2,3] // // Example 2: // Input: root = [] // Output: [] // // Example 3: // Input: root = [1] // Output: [1] // // Example 4: // Input: root = [1,2] // Output: [1,2] // // Example 5: // Input: root = [1,null,2] // Output: [1,2] // // Constraints: // The number of nodes in the tree is in the range [0, 100]. // -100 <= Node.val <= 100 // // Follow up: Recursive solution is trivial, could you do it iteratively? // Related Topics 栈 树 // 👍 549 👎 0
//leetcode submit region begin(Prohibit modification and deletion) /** * Definition for a binary tree node. */ publicclassTreeNode{ 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; } } classSolution{
List<Integer> ret = new ArrayList<>(); public List<Integer> preorderTraversal(TreeNode root){ preOrder(root); return ret; }
publicvoidpreOrder(TreeNode root){ if (root != null) { ret.add(root.val); preOrder(root.left); preOrder(root.right); } } } //leetcode submit region end(Prohibit modification and deletion)
publicclassNAryTreePreorderTraversal{ publicstaticvoidmain(String[] args){ Solution s = new NAryTreePreorderTraversal().new Solution(); }
// Given the root of an n-ary tree, return the preorder traversal of its nodes' values. // Nary-Tree input serialization is represented in their level order traversal. //Each group of children is separated by the null value (See examples) // Example 1: //Input: root = [1,null,3,2,4,null,5,6] //Output: [1,3,5,6,2,4] // Example 2: //Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null //,12,null,13,null,null,14] //Output: [1,2,3,6,7,11,14,4,8,12,5,9,13,10] // // Constraints: // The number of nodes in the tree is in the range [0, 104]. // 0 <= Node.val <= 104 // The height of the n-ary tree is less than or equal to 1000. // // Follow up: Recursive solution is trivial, could you do it iteratively? // Related Topics 树 // 👍 152 👎 0
//leetcode submit region begin(Prohibit modification and deletion) // Definition for a Node. classNode{ publicint val; public List<Node> children;
publicNode(){}
publicNode(int _val){ val = _val; }
publicNode(int _val, List<Node> _children){ val = _val; children = _children; } }
classSolution{ List<Integer> ret = new ArrayList<>();
public List<Integer> preorder(Node root){ preOrder(root); return ret; }
publicvoidpreOrder(Node root){ if (root != null) { ret.add(root.val); for (Node n: root.children) { preOrder(n); } } } } //leetcode submit region end(Prohibit modification and deletion)
publicclassInvertBinaryTree{ publicstaticvoidmain(String[] args){ Solution s = new InvertBinaryTree().new Solution(); }
// Given the root of a binary tree, invert the tree, and return its root. // Example 1: //Input: root = [4,2,7,1,3,6,9] //Output: [4,7,2,9,6,3,1] // Example 2: //Input: root = [2,1,3] //Output: [2,3,1] // Example 3: //Input: root = [] //Output: [] // // Constraints: // The number of nodes in the tree is in the range [0, 100]. // -100 <= Node.val <= 100 // // Related Topics 树 // 👍 807 👎 0
//leetcode submit region begin(Prohibit modification and deletion) /** * Definition for a binary tree node. */
publicclassCongShangDaoXiaDaYinErChaShuIiLcof{ publicstaticvoidmain(String[] args){ Solution s = new CongShangDaoXiaDaYinErChaShuIiLcof().new Solution(); }
//English description is not available for the problem. Please switch to Chinese //. Related Topics 树 广度优先搜索 // 👍 95 👎 0
//leetcode submit region begin(Prohibit modification and deletion) /** * Definition for a binary tree node. */ publicclassTreeNode{ int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } }
classSolution{
List<List<Integer>> liiRet = new ArrayList<>(); public List<List<Integer>> levelOrder(TreeNode root) {
signKLevel(root, 0); return liiRet; }
publicvoidsignKLevel(TreeNode root, int k){ if (root != null) { if (liiRet.size() == k) { liiRet.add(new ArrayList<>()); } liiRet.get(k).add(root.val); signKLevel(root.left, k+1); signKLevel(root.right, k+1); } } } //leetcode submit region end(Prohibit modification and deletion) }
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ var ret [][]int funclevelOrder(root *TreeNode) [][]int { // write code here ret = make([][]int, 0) if root == nil { return ret } ret = append(ret, []int{}) travelTree(root, 0) return ret }
functravelTree(root *TreeNode, k int) { if root == nil { return } iflen(ret) == k { ret = append(ret, []int{}) } ret[k] = append(ret[k], root.Val) travelTree(root.Left, k+1) travelTree(root.Right, k+1) }
publicclassBinaryTreeLevelOrderTraversalIi{ publicstaticvoidmain(String[] args){ Solution s = new BinaryTreeLevelOrderTraversalIi().new Solution(); }
//Given the root of a binary tree, return the bottom-up level order traversal of its nodes' values. (i.e., from left to right, level by level from leaf to root). // Example 1: //Input: root = [3,9,20,null,null,15,7] //Output: [[15,7],[9,20],[3]] // Example 2: //Input: root = [1] //Output: [[1]] // Example 3: //Input: root = [] //Output: [] // // Constraints: // The number of nodes in the tree is in the range [0, 2000]. // -1000 <= Node.val <= 1000 // // Related Topics 树 广度优先搜索 // 👍 423 👎 0
//leetcode submit region begin(Prohibit modification and deletion) /** * Definition for a binary tree node. */ publicclassTreeNode{ 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; } }
classSolution{
List<List<Integer>> liiRet = new ArrayList<>(); public List<List<Integer>> levelOrderBottom(TreeNode root) { signKLevel(root, 0); for (int i = 0, j=liiRet.size()-1; i < j; i++, j--) { List<Integer> tmp = liiRet.get(i); liiRet.set(i, liiRet.get(j)); liiRet.set(j, tmp); } return liiRet; }
publicvoidsignKLevel(TreeNode root, int k){
if (root != null) { if (liiRet.size() == k) { liiRet.add(new ArrayList<>()); } liiRet.get(k).add(root.val); signKLevel(root.left, k+1); signKLevel(root.right, k+1); } } } //leetcode submit region end(Prohibit modification and deletion) }
publicclassBinaryTreeZigzagLevelOrderTraversal{ publicstaticvoidmain(String[] args){ Solution s = new BinaryTreeZigzagLevelOrderTraversal().new Solution(); }
//Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between). // Example 1: //Input: root = [3,9,20,null,null,15,7] //Output: [[3],[20,9],[15,7]] // Example 2: //Input: root = [1] //Output: [[1]] // Example 3: //Input: root = [] //Output: [] // // Constraints: // The number of nodes in the tree is in the range [0, 2000]. // -100 <= Node.val <= 100 // // Related Topics 栈 树 广度优先搜索 // 👍 424 👎 0
//leetcode submit region begin(Prohibit modification and deletion) /** * Definition for a binary tree node. */ publicclassTreeNode{ 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; } }
classSolution{
List<List<Integer>> liiRet = new ArrayList<>(); public List<List<Integer>> zigzagLevelOrder(TreeNode root) { signKLevel(root, 0); for (int k = 0; k < liiRet.size(); k++) { if (k%2 == 1) {
for (int i = 0, j=liiRet.get(k).size()-1; i < j; i++, j--) { int tmp = liiRet.get(k).get(i); liiRet.get(k).set(i, liiRet.get(k).get(j)); liiRet.get(k).set(j, tmp); } } } return liiRet; }
publicvoidsignKLevel(TreeNode root, int k){
if (root != null) { if (liiRet.size() == k) { liiRet.add(new ArrayList<>()); } liiRet.get(k).add(root.val); signKLevel(root.left, k+1); signKLevel(root.right, k+1); } } } //leetcode submit region end(Prohibit modification and deletion)
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */
var ret [][]int
funczigzagLevelOrder(root *TreeNode) [][]int { // write code here ret = make([][]int, 0) if root == nil { return ret } ret = append(ret, []int{}) travelTree(root, 0) for i, v := range ret { if i% 2 == 1 { for j, k := 0, len(v)-1; j<k; j, k = j+1, k-1 { v[j], v[k] = v[k], v[j] } } } return ret }
functravelTree(root *TreeNode, k int) { if root == nil { return } iflen(ret) == k { ret = append(ret, []int{}) } ret[k] = append(ret[k], root.Val) travelTree(root.Left, k+1) travelTree(root.Right, k+1) }
self.lii_ret[k].append(root.val) self.sign_k_level(root.left, k + 1) self.sign_k_level(root.right, k + 1)
defzigzagLevelOrder(self, root: TreeNode) -> List[List[int]]: self.sign_k_level(root, 0) for i inrange(0, len(self.lii_ret)): if i % 2 == 1: self.lii_ret[i].reverse() return self.lii_ret
# leetcode submit region end(Prohibit modification and deletion)
publicclassBalancedBinaryTree{ publicstaticvoidmain(String[] args){ Solution s = new BalancedBinaryTree().new Solution(); }
//Given a binary tree, determine if it is height-balanced. // For this problem, a height-balanced binary tree is defined as: // a binary tree in which the left and right subtrees of every node differ in height by no more than 1. // Example 1: //Input: root = [3,9,20,null,null,15,7] //Output: true // Example 2: //Input: root = [1,2,2,3,3,null,null,4,4] //Output: false // Example 3: //Input: root = [] //Output: true // Constraints: // The number of nodes in the tree is in the range [0, 5000]. // -104 <= Node.val <= 104 // // Related Topics 树 深度优先搜索 递归 // 👍 652 👎 0
//leetcode submit region begin(Prohibit modification and deletion) /** * Definition for a binary tree node. * */
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ funcisBalanced(root *TreeNode)bool { return treeHeight(root) >= 0 }
functreeHeight(root *TreeNode)int { if root == nil { return0 } l := treeHeight(root.Left) r := treeHeight(root.Right)
if l < 0 || r < 0 || math.Abs(float64(l-r)) > 1 { return-1 } returnint(math.Max(float64(l), float64(r))) + 1 }
l, r = self.get_tree_height(root.left), self.get_tree_height(root.right) if l < 0or r < 0orabs(l-r) > 1: return -1 returnmax(l, r) + 1 # leetcode submit region end(Prohibit modification and deletion)