LeetCode 题解(132): Binary Tree Right Side View
来源:互联网 发布:c语言的思考图 编辑:程序博客网 时间:2024/06/05 17:19
题目:
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 <--- / \2 3 <--- \ \ 5 4 <---
You should return [1, 3, 4]
.
比较简单,前序右子树先 遍历,用一个全局变量记录当前的最大高度,如果比当前最大高度高,加入到result中,同时更新最大高度。
C++版:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int h; vector<int> rightSideView(TreeNode* root) { vector<int> result; if(root == NULL) return result; h = 0; int height = 1; if(height > h) { result.push_back(root->val); h = height; } visit(height+1, root->right, result); visit(height+1, root->left, result); return result; } void visit(int height, TreeNode* root, vector<int>& result) { if(root == NULL) return; if(height > h) { result.push_back(root->val); h = height; } visit(height+1, root->right, result); visit(height+1, root->left, result); }};
Java版:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int h; public List<Integer> rightSideView(TreeNode root) { List<Integer> result = new ArrayList<>(); if(root == null) return result; h = 0; int height = 1; if(height > h) { result.add(root.val); h = height; } visit(height+1, root.right, result); visit(height+1, root.left, result); return result; } public void visit(int height, TreeNode root, List<Integer> result) { if(root == null) return; if(height > h) { result.add(root.val); h = height; } visit(height+1, root.right, result); visit(height+1, root.left, result); }}
Python版:
# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: # @param {TreeNode} root # @return {integer[]} def __init__(self): self.h = 0 def rightSideView(self, root): result = [] if root == None: return [] height = 1 if height > self.h: result.append(root.val) self.h = height self.visit(height+1, root.right, result) self.visit(height+1, root.left, result) return result def visit(self, height, root, result): if root == None: return if height > self.h: result.append(root.val) self.h = height self.visit(height+1, root.right, result) self.visit(height+1, root.left, result)
0 0
- LeetCode 题解(132): Binary Tree Right Side View
- LeetCode题解-199-Binary Tree Right Side View
- LeetCode 199. Binary Tree Right Side View 题解
- 【题解】Binary Tree Right Side View
- [leetcode][tree] Binary Tree Right Side View
- LeetCode OJ Binary Tree Right Side View
- LeetCode:Binary Tree Right Side View
- [leetcode] 199 Binary Tree Right Side View
- [LeetCode]199.Binary Tree Right Side View
- Leetcode: Binary Tree Right Side View
- LeetCode | Binary Tree Right Side View
- [LeetCode] Binary Tree Right Side View
- LeetCode: Binary Tree Right Side View
- [leetcode]48 Binary Tree Right Side View
- [leetcode] Binary Tree Right Side View
- leetcode-199 Binary Tree Right Side View
- [LeetCode]Binary Tree Right Side View
- 199Binary Tree Right Side View-LeetCode
- #leetcode#Longest Palindromic Substring
- C语言实现红黑树
- 基于 JVMTI 实现 Java 线程的监控
- 深入 Java 调试体系,第 2 部分: JVMTI 和 Agent 实现
- Hprof使用及在Hadoop中MR任务使用
- LeetCode 题解(132): Binary Tree Right Side View
- summary ranges
- PMC Flashtec + Memblaze PBlaze 4 = 性能惊人的NVM Express SSD!
- House Robber
- LeetCode 题解(133): Repeated DNA Sequences
- House Robber II
- 点赞和吐糟Adblock Plus~进阶教程
- TCP/IP VLSM
- hdu 5270 ZYB loves Xor II