通常,一个“bottom-up”函数bottom_up(root)如下所示: 1.returnspecific valuefornull node2. left_ans = bottom_up(root.left) // call function recursivelyforleft child3. right_ans = bottom_up(root.right) // call function recu
Binary Tree Right Side View 中文网站:199. 二叉树的右视图 问题描述 Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. Example 1: Input: root = [1,2,3,null,5,null,4] Output: ...
今天和大家聊的问题叫做 二叉树的右视图,我们先来看题面:https://leetcode-cn.com/problems/binary-tree-right-side-view/ Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. 题意 给定一...
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; } } 1. 2. 3. 4. ...
classSolution{public:vector<vector<int>> levelOrderBottom(TreeNode* root) {//创建 队列queue<TreeNode*> que;//创建二维数组保存各层遍历结果vector<vector<int>> res;//判断根节点是否为空,不为空加入根节点if(root !=NULL) que.push(root);//开始遍历,循环条件为队列是否为空while(!(que.empty())...
Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example: Given the following binary tree, 1 <--- / \ ...
513 Find Bottom Left Tree Value 55.60% Medium 510 Inorder Successor in BST II $ 56.20% Medium 509 Fibonacci Number 66.40% Easy 508 Most Frequent Subtree Sum 52.00% Medium 507 Perfect Number 32.70% Easy 506 Relative Ranks 48.50% Easy 505 The Maze II 34.80% Medium 504 Base 7 46.40% Easy ...
26 SubstructureInTree 树的子结构 Java 27 MirrorOfBinaryTree 二叉树的镜像 Java 28 SymmetricalBinaryTree 对称的二叉树 Java 29 PrintMatrix 顺时针打印矩阵 Java 30 MinInStack 包含min 函数的栈 Java 31 StackPushPopOrder 栈的压入、弹出序列 Java 32_01 PrintTreeFromTopToBottom 不分行从上往下打印二叉树...
95. Unique Binary Search Trees II 递归的思路,对于1..n,我们分别以1到n为根结点,然后左边的树构建左子树,右边的树构建右子树,然后左右子树两两结合,这个过程可以用递归来完成。 /** * Definition for a binary tree node. * public class TreeNode { ...
238. Product of Array Except Self tag:使用变量代替单调栈dp数组 同国版1762。 199. Binary Tree Right Side View tag:bfs进行level order traversal 经典的 bfs level order traversal。 301. Remove Invalid Parentheses tag:带有剪枝的 dfs 由于题目中要求求出所有可能的情况,因此算法上选择 dfs。首先定义好 ...