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
  • 相关阅读:
    POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】
    HDU 5687 Problem C 【字典树删除】
    HDU 1298 T9【字典树增加||查询】
    AIM Tech Round 5 (rated, Div. 1 + Div. 2) C. Rectangles 【矩阵交集】
    基本数据结构:栈-队列-双向队列
    Elasticsearch 环境准备
    django项目部署
    linux基础
    购物车
    Python内置函数
  • 原文地址:https://www.cnblogs.com/freeneng/p/3220947.html
Copyright © 2011-2022 走看看