We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
给定一个二叉树,返回它的 后序 遍历。
Input: root = [1,null,2,3] Output: [3,2,1]
Input: root = [] Output: []
Input: root = [1] Output: [1]
Input: root = [1,2] Output: [2,1]
Input: root = [1,null,2] Output: [2,1]
[0, 100]
-100 <= Node.val <= 100
The text was updated successfully, but these errors were encountered:
/** * 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; };
/** * 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; };
Sorry, something went wrong.
No branches or pull requests
145. Binary Tree Postorder Traversal
给定一个二叉树,返回它的 后序 遍历。
Example 1
Example 2
Example 3
Example 4
Example 5
Note
[0, 100]
.-100 <= Node.val <= 100
The text was updated successfully, but these errors were encountered: