Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

145. Binary Tree Postorder Traversal #334

Open
Tcdian opened this issue Sep 29, 2020 · 1 comment
Open

145. Binary Tree Postorder Traversal #334

Tcdian opened this issue Sep 29, 2020 · 1 comment
Labels

Comments

@Tcdian
Copy link
Owner

Tcdian commented Sep 29, 2020

145. Binary Tree Postorder Traversal

给定一个二叉树,返回它的 后序 遍历。

Example 1

Input: root = [1,null,2,3]
Output: [3,2,1]

Example 2

Input: root = []
Output: []

Example 3

Input: root = [1]
Output: [1]

Example 4

Input: root = [1,2]
Output: [2,1]

Example 5

Input: root = [1,null,2]
Output: [2,1]

Note

  • The number of the nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100
@Tcdian
Copy link
Owner Author

Tcdian commented Sep 29, 2020

Solution

  • JavaScript Solution
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var postorderTraversal = function(root) {
    if (root === null) {
        return [];
    }
    const queue = [];
    let patrol = root;
    const result = [];
    while (patrol !== null || queue.length !== 0) {
        while (patrol !== null) {
            queue.push([patrol, 0]);
            patrol = patrol.left;
        }
        let pair = queue.pop();
        if (pair[1] === 1) {
            result.push(pair[0].val);
        } else {
            pair[1] += 1;
            queue.push(pair);
            patrol = pair[0].right;
        }
    }
    return result;
};
  • TypeScript Solution
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

function postorderTraversal(root: TreeNode | null): number[] {
    if (root === null) {
        return [];
    }
    const queue: [TreeNode, number][] = [];
    let patrol: TreeNode | null = root;
    const result: number[] = [];
    while (patrol !== null || queue.length !== 0) {
        while (patrol !== null) {
            queue.push([patrol, 0]);
            patrol = patrol.left;
        }
        let pair = queue.pop()!;
        if (pair[1] === 1) {
            result.push(pair[0].val);
        } else {
            pair[1] += 1;
            queue.push(pair);
            patrol = pair[0].right;
        }
    }
    return result;
};

@Tcdian Tcdian removed the Classic label Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant