跳转至

94.Binary Tree Inorder Traversal

Tags: Medium Tree

Link: https://leetcode.com/problems/binary-tree-inorder-traversal/


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

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3

Output: [1,3,2]

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


Answer:

递归解法:

/**
 * 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:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        if (!root) return res;

        inorderTraversal(root, res);

        return res;
    }

    void inorderTraversal(TreeNode * root, vector<int> & res) {
        if (!root) return;

        if (root -> left) inorderTraversal(root -> left, res);
        res.push_back(root -> val);
        if (root -> right) inorderTraversal(root -> right, res);
    }
};

使用栈的解法:

/**
 * 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:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        if (!root) return res;

        stack<TreeNode *> s;
        TreeNode *p = root;

        while (!s.empty() || p != nullptr) {
            if (p != nullptr) {
                s.push(p);
                p = p -> left;
            }
            else {
                p = s.top();
                s.pop();
                res.push_back(p -> val);
                p = p -> right;
            }
        }

        return res;
    }
};

线索二叉树方法:

/**
 * 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:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        if (!root) return res;

        TreeNode * cur = root, *pre = nullptr;

        while (cur != nullptr) {
            if (cur -> left == nullptr) {
                res.push_back(cur -> val);
                cur  = cur -> right;
            }
            else {
                pre = cur -> left;
                while (pre -> right != nullptr && pre -> right != cur)
                    pre = pre -> right;

                if (pre -> right == nullptr) {
                    pre -> right = cur;
                    cur = cur -> left;
                }
                else {
                    res.push_back(cur -> val);
                    pre -> right = nullptr;
                    cur = cur -> right;
                }
            }
        }

        return res;
    }
};
  1. 如果当前节点的左孩子为空,则输出当前节点并将其右孩子作为当前节点。

  2. 如果当前节点的左孩子不为空,在当前节点的左子树中找到当前节点在中序遍历下的前驱节点。

    a) 如果前驱节点的右孩子为空,将它的右孩子设置为当前节点。当前节点更新为当前节点的左孩子。

    b) 如果前驱节点的右孩子为当前节点,将它的右孩子重新设为空(恢复树的形状)。输出当前节点。当前节点更新为当前节点的右孩子。

  3. 重复以上1、2直到当前节点为空。