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
原创粉丝点击