zoukankan      html  css  js  c++  java
  • LeetCode Binary Tree Right Side View (DFS/BFS)

    题意:

      给一棵二叉树,要求收集每层的最后一个节点的值。按从顶到底装进vector返回。

    思路:

      BFS比较简单,先遍历右孩子就行了。

     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     vector<int> rightSideView(TreeNode* root) {
    13         if(root==NULL)    return vector<int>();
    14         deque<TreeNode*> que(1,root);
    15         vector<int> ans;
    16 
    17         while(!que.empty())
    18         {
    19             ans.push_back(que.front()->val);
    20             for(int i=que.size(); i>0; i--)
    21             {
    22                 TreeNode* p=que.front();
    23                 que.pop_front();
    24                 if(p->right)    que.push_back(p->right);
    25                 if(p->left)        que.push_back(p->left);
    26             }
    27         }
    28         return ans;
    29     }
    30 };
    AC代码

      DFS比较技巧,需要知道其层次。

     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     vector<int> rightSideView(TreeNode* root) {
    13         vector<int> ans;
    14         DFS(ans,root,1);    
    15         return ans;
    16     }
    17     void DFS(vector<int>& ans,TreeNode* t,int depth)
    18     {
    19         if(t==NULL)    return;
    20         if(depth>ans.size())    ans.push_back(t->val);
    21         DFS(ans,t->right,depth+1);
    22         DFS(ans,t->left,depth+1);
    23     }
    24 };
    AC代码
  • 相关阅读:
    jQuery 插件开发——Menu(导航菜单)
    jQuery 插件开发——PopupLayer(弹出层)
    jQuery 插件开发——GridData(表格)
    angularjs+requlirejs 搭建前端框架(1)
    探讨js闭包
    python的特殊方法介绍
    收集TCP端口的访问延迟和丢包率
    【IT界的厨子】酱香鲈鱼
    Centos 6.5 安装Python 3.7
    python技巧 python2中的除法结果为0
  • 原文地址:https://www.cnblogs.com/xcw0754/p/4972800.html
Copyright © 2011-2022 走看看