zoukankan      html  css  js  c++  java
  • [Leetcode 94] 72 Binary Tree Inorder Traversal

    Problem:

    Given a binary tree, return the inorder traversal of its nodes' values.

    For example:
    Given binary tree {1,#,2,3},

       1
        
         2
        /
       3
    

    return [1,3,2].

    Note: Recursive solution is trivial, could you do it iteratively?

    Analysis:

    Basic tree traversal problems. Recursive solution is just to proper arrange the visiting order of the tree. Iterative version need the help of stack. My own solution is that everytime pop a node from the stack, if it has no children nodes, then record it in the result vector. Else we must first push it right child node,  then itself and at last its left child node. If any of the node is NULL, skip the pushing operation. Another thing to do is set the node's left and right pointer to NULL.

    Code:

    Recursive Version:

     1 /**
     2  * Definition for binary tree
     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> res;
    13 
    14     vector<int> inorderTraversal(TreeNode *root) {
    15         // Start typing your C/C++ solution below
    16         // DO NOT write int main() function
    17         res.clear();
    18         
    19         inorder(root);
    20         
    21         return res;
    22     }
    23     
    24     
    25 private:
    26     void inorder(TreeNode *node) {
    27         if (node == NULL)
    28             return ;
    29             
    30         inorder(node->left);
    31         res.push_back(node->val);
    32         inorder(node->right);
    33     }
    34 };
    View Code

    Iterative Version:

     1 /**
     2  * Definition for binary tree
     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> inorderTraversal(TreeNode *root) {
    13         // Start typing your C/C++ solution below
    14         // DO NOT write int main() function
    15         vector<int> res;
    16         
    17         if (root == NULL)
    18             return res;
    19         
    20         stack<TreeNode *> stk;
    21         TreeNode *tmp, *node;
    22         stk.push(root);
    23         do {
    24             node = stk.top();
    25             stk.pop();
    26             
    27             if (node->left == NULL && node->right == NULL) {
    28                 res.push_back(node->val);
    29                 continue;
    30             }
    31             
    32             if (node->right != NULL) {
    33                 stk.push(node->right);
    34                 node->right = NULL;
    35             }
    36             
    37             if (node->left != NULL) {
    38                 tmp = node->left;
    39                 node->left = NULL;
    40                 stk.push(node);
    41                 stk.push(tmp);
    42             } else 
    43                 stk.push(node);
    44             
    45         } while (!stk.empty());
    46         
    47         
    48         return res;
    49     }
    50 };
    View Code
  • 相关阅读:
    SWT界面刷新
    如何查看SWT源代码和帮助文档
    五分钟带你搞懂子网掩码
    python操作excel实用脚本
    Maven多镜像配置
    FrameWork数据权限浅析4之基于多维度配置表实现行级数据安全
    FrameWork数据权限浅析3之基于角色的配置表实现行级数据安全
    FrameWork数据权限浅析2之基于用户的配置表实现行级数据安全
    FrameWork数据权限浅析1之基于手工修改模型实现行级数据安全
    Framework元数据向导错误之BMT-MD-6001与BMT-IMP-0002
  • 原文地址:https://www.cnblogs.com/freeneng/p/3220947.html
Copyright © 2011-2022 走看看