zoukankan      html  css  js  c++  java
  • Leetcode199. Binary Tree Right Side View二叉树的右视图

    给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。

    示例:

    输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释:

    先求深度,中序遍历或者前序遍历都可以

    class Solution {
    public:
        vector<int> v;
        vector<int> rightSideView(TreeNode* root) 
        {
            int len = GetDepth(root);
            if(len == 0)
                return v;
            v = vector<int>(len , 0);
            GetAns(root, 0);
            return v;
    
        }
    
        void GetAns(TreeNode* root, int depth)
        {
            if(root == NULL)
                return;
            v[depth] = root ->val;
            GetAns(root ->left, depth + 1);
            GetAns(root ->right, depth + 1);
        }
    
        int GetDepth(TreeNode *root)
        {
            if(root == NULL)
                return 0;
            return 1 + max(GetDepth(root ->left), GetDepth(root ->right));
        }
    };
  • 相关阅读:
    zbb20180930 Postman 使用方法详解
    Cookie、Session、jsp、EL、JSTL
    Http协议、Tomcat、servlet
    xml、网络编程、 反射
    JDBC、DBUtils
    Java IO流对象、多线程
    mySql
    Java基础
    VueJs
    GIT
  • 原文地址:https://www.cnblogs.com/lMonster81/p/10433821.html
Copyright © 2011-2022 走看看