Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] BFS: vector<vect...
1classSolution {2public:3vector<int> preorderTraversal(TreeNode *root) {4vector<int>result;5TreeNode *p =root;6while(p) {7if(!p->left) {8result.push_back(p->val);9p = p->right;10}else{11TreeNode *q = p->left;12while(q->right && q->right !=p) {13q = q->right;14}1...
public void flatten(TreeNode root) { if(root==null) return; TreeNode curr = root; while(curr!=null){ //保存右子树, 后面用 TreeNode oldRight = curr.right; //左子树不为空, 执行一系列改变指向的操作 if(curr.left!=null){ //找到左子树的最右节点 TreeNode rightMost = curr.left; while(...
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow...
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal Description Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given ...
LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal Description Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, given inorder = [9,3,15,20,7]postorder = [9,15,7...
Can you solve this real interview question? Lowest Common Ancestor of a Binary Tree - Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia [https://en.wikipedia.org/wi
Given the root of a binary tree, return the inorder traversal of its nodes' values. Example 1: Input: root = [1,null,2,3] Output: [1,3,2] Explanation: Example 2: Input: root = [1,2,3,4,5,null,8,null,null,6,7,9] Output: [4,2,6,5,7,1,3,9,8] Explanation: Example...
简单 95. 不同的二叉搜索树 II 74.6% 中等 96. 不同的二叉搜索树 71.5% 中等 98. 验证二叉搜索树 39.3% 中等 99. 恢复二叉搜索树 61.3% 中等 100. 相同的树 63.2% 简单 101. 对称二叉树 62.3% 简单 102. 二叉树的层序遍历 69.7% 中等 103. 二叉树的锯齿形层序遍历 60.2% 中等 104. 二叉树的最大...
968. 监控二叉树 - 给定一个二叉树,我们在树的节点上安装摄像头。 节点上的每个摄影头都可以监视其父对象、自身及其直接子对象。 计算监控树的所有节点所需的最小摄像头数量。 示例 1: [https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/29/bst_c