class Solution { private int max = 0; public int diameterOfBinaryTree(TreeNode root) { maxDepth(root); return max; } private int maxDepth(TreeNode root){ if(root == null) return 0; int l = maxDepth(root.left); int r = maxDepth(root.right); max = Math....
英文网站:199. Binary Tree Right Side View 中文网站:199. 二叉树的右视图 问题描述 Given therootof a binary tree, imagine yourself standing on theright sideof it, returnthe values of the nodes you can see ordered from top to bottom. Example 1: Input: root = [1,2,3,null,5,null,4] O...
LeetCode OJ:Binary Tree Right Side View(右侧视角下的二叉树) Given a binary tree, imagine yourself standing on therightside of it, return the values of the nodes you can see ordered from top to bottom. For example: Given the following binary tree, 1 <--- / \ 2 3 <--- \ \ 5 4...
3.Binary Tree Level Order Traversal II(https://leetcode.com/problems/binary-tree-level-order-traversal-ii/description/) classSolution {public: vector<vector<int>> levelOrderBottom(TreeNode*root) { vector<vector<int>>res;if(root == NULL)returnres; vector<int>layer; queue<TreeNode*>q; q.p...
[LeetCode]Binary Tree Right Side View Question 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,...
题目:Binary Tree Level Order Traversal II - LeetCode 反转一下最终的顺序就好 代码: class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right class Solution: def levelOrderBottom(self, root: TreeNode) -> List[List[int]...
和上题一样,只需要最后将结果集逆置一下就可以了vector<vector<int>> levelOrderBottom(TreeNode* root) { vector<vector<int>> res; if(root == nullptr) return res; queue<TreeNode*> que; que.push(root); vector<int> temp; while(!que.empty()){ auto size = que.size(); while(...
199 Binary Tree Right Side View 二叉树的右视图 Description: 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. Example: Input: [1,2,3,null,5,null,4] ...
https://leetcode.com/problems/binary-tree-level-order-traversal-ii 问题: 思路:只要在普通的层序遍历代码中修改一处:results.add(0, level);,这样每次头插到结果中。 publicList<List<Integer>>levelOrderBottom(TreeNoderoot){List<List<Integer>>results=newArrayList<>();if(root==null)returnresults;Queue...
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. 5. 6.