René's URL Explorer Experiment


Title: [LeetCode] 145. Binary Tree Postorder Traversal · Issue #145 · grandyang/leetcode · GitHub

Open Graph Title: [LeetCode] 145. Binary Tree Postorder Traversal · Issue #145 · grandyang/leetcode

X Title: [LeetCode] 145. Binary Tree Postorder Traversal · Issue #145 · grandyang/leetcode

Description: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 经典题目,求二叉树的后序遍历的非递归方法,跟前序,中序,层...

Open Graph Description: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it it...

X Description: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do i...

Opengraph URL: https://github.com/grandyang/leetcode/issues/145

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[LeetCode] 145. Binary Tree Postorder Traversal","articleBody":" \n\nGiven a binary tree, return the _postorder_ traversal of its nodes' values.\n\nFor example:  \nGiven binary tree `{1,#,2,3}`,  \n\n    \n    \n       1\n        \\\n         2\n        /\n       3\n    \n\nreturn `[3,2,1]`.\n\n**Note:** Recursive solution is trivial, could you do it iteratively?\n\n \n\n经典题目,求二叉树的后序遍历的非递归方法,跟前序,中序,层序一样都需要用到栈,后序的顺序是左-右-根,所以当一个结点值被取出来时,它的左右子结点要么不存在,要么已经被访问过了。先将根结点压入栈,然后定义一个辅助结点 head,while 循环的条件是栈不为空,在循环中,首先将栈顶结点t取出来,如果栈顶结点没有左右子结点,或者其左子结点是 head,或者其右子结点是 head 的情况下。将栈顶结点值加入结果 res 中,并将栈顶元素移出栈,然后将 head 指向栈顶元素;否则的话就看如果右子结点不为空,将其加入栈,再看左子结点不为空的话,就加入栈,注意这里先右后左的顺序是因为栈的后入先出的特点,可以使得左子结点先被处理。下面来看为什么是这三个条件呢,首先如果栈顶元素如果没有左右子结点的话,说明其是叶结点,而且入栈顺序保证了左子结点先被处理,所以此时的结点值就可以直接加入结果 res 了,然后移出栈,将 head 指向这个叶结点,这样的话 head 每次就是指向前一个处理过并且加入结果 res 的结点,那么如果栈顶结点的左子结点或者右子结点是 head 的话,说明其子结点已经加入结果 res 了,那么就可以处理当前结点了。\n\n看到这里,大家可能对 head 的作用,以及为何要初始化为 root,还不是很清楚,这里再解释一下。head 是指向上一个被遍历完成的结点,由于后序遍历的顺序是左-右-根,所以一定会一直将结点压入栈,一直到把最左子结点(或是最左子结点的最右子结点)压入栈后,开始进行处理。一旦开始处理了,head 就会被重新赋值。所以 head 初始化值并没有太大的影响,唯一要注意的是不能初始化为空,因为在判断是否打印出当前结点时除了判断是否是叶结点,还要看 head 是否指向其左右子结点,如果 head 指向左子结点,那么右子结点一定为空,因为入栈顺序是根-右-左,不存在右子结点还没处理,就直接去处理根结点了的情况。若 head 指向右子结点,则是正常的左-右-根的处理顺序。那么回过头来在看,若 head 初始化为空,且此时正好左子结点不存在,那么在压入根结点时,head 和左子结点相等就成立了,此时就直接打印根结点了,明显是错的。所以 head 只要不初始化为空,一切都好说,甚至可以新建一个结点也没问题。将 head 初始化为 root,也可以,就算只有一个 root 结点,那么在判定叶结点时就将 root 打印了,然后就跳出 while 循环了,也不会出错。代码如下:\n\n \n\n解法一:\n    \n    \n    class Solution {\n    public:\n        vector\u003cint\u003e postorderTraversal(TreeNode* root) {\n            if (!root) return {};\n            vector\u003cint\u003e res;\n            stack\u003cTreeNode*\u003e s{{root}};\n            TreeNode *head = root;\n            while (!s.empty()) {\n                TreeNode *t = s.top();\n                if ((!t-\u003eleft \u0026\u0026 !t-\u003eright) || t-\u003eleft == head || t-\u003eright == head) {\n                    res.push_back(t-\u003eval);\n                    s.pop();\n                    head = t;\n                } else {\n                    if (t-\u003eright) s.push(t-\u003eright);\n                    if (t-\u003eleft) s.push(t-\u003eleft);\n                }\n            }\n            return res;\n        }\n    };\n\n \n\n由于后序遍历的顺序是左-右-根,而先序遍历的顺序是根-左-右,二者其实还是很相近的,可以先在先序遍历的方法上做些小改动,使其遍历顺序变为根-右-左,然后翻转一下,就是左-右-根啦,翻转的方法我们使用反向Q,哦不,是反向加入结果 res,每次都在结果 res 的开头加入结点值,而改变先序遍历的顺序就只要该遍历一下入栈顺序,先左后右,这样出栈处理的时候就是先右后左啦,参见代码如下:\n\n \n\n解法二:\n    \n    \n    class Solution {\n    public:\n        vector\u003cint\u003e postorderTraversal(TreeNode* root) {\n            if (!root) return {};\n            vector\u003cint\u003e res;\n            stack\u003cTreeNode*\u003e s{{root}};\n            while (!s.empty()) {\n                TreeNode *t = s.top(); s.pop();\n                res.insert(res.begin(), t-\u003eval);\n                if (t-\u003eleft) s.push(t-\u003eleft);\n                if (t-\u003eright) s.push(t-\u003eright);\n            }\n            return res;\n        }\n    };\n\n \n\n那么在 [Binary Tree Preorder Traversal](http://www.cnblogs.com/grandyang/p/4146981.html) 中的解法二也可以改动一下变成后序遍历,改动的思路跟上面的解法一样,都是先将先序遍历的根-左-右顺序变为根-右-左,再翻转变为后序遍历的左-右-根,翻转还是改变结果 res 的加入顺序,然后把更新辅助结点p的左右顺序换一下即可,代码如下:\n\n \n\n解法三:\n    \n    \n    class Solution {\n    public:\n        vector\u003cint\u003e postorderTraversal(TreeNode* root) {\n            vector\u003cint\u003e res;\n            stack\u003cTreeNode*\u003e s;\n            TreeNode *p = root;\n            while (!s.empty() || p) {\n                if (p) {\n                    s.push(p);\n                    res.insert(res.begin(), p-\u003eval);\n                    p = p-\u003eright;\n                } else {\n                    TreeNode *t = s.top(); s.pop();\n                    p = t-\u003eleft;\n                }\n            }\n            return res;\n        }\n    };\n\n \n\n论坛上还有一种双栈的解法,其实本质上跟解法二没什么区别,都是利用了改变先序遍历的顺序来实现后序遍历的,参见代码如下:\n\n \n\n解法四:\n    \n    \n    class Solution {\n    public:\n        vector\u003cint\u003e postorderTraversal(TreeNode* root) {\n            if (!root) return {};\n            vector\u003cint\u003e res;\n            stack\u003cTreeNode*\u003e s1, s2;\n            s1.push(root);\n            while (!s1.empty()) {\n                TreeNode *t = s1.top(); s1.pop();\n                s2.push(t);\n                if (t-\u003eleft) s1.push(t-\u003eleft);\n                if (t-\u003eright) s1.push(t-\u003eright);\n            }\n            while (!s2.empty()) {\n                res.push_back(s2.top()-\u003eval); s2.pop();\n            }\n            return res;\n        }\n    };\n\n \n\nGithub 同步地址:\n\n\u003chttps://github.com/grandyang/leetcode/issues/145\u003e\n\n \n\n类似题目:\n\n[Binary Tree Preorder Traversal](http://www.cnblogs.com/grandyang/p/4146981.html)\n\n[Binary Tree Inorder Traversal](http://www.cnblogs.com/grandyang/p/4297300.html)\n\n[Binary Tree Level Order Traversal](http://www.cnblogs.com/grandyang/p/4051321.html)\n\n \n\n参考资料:\n\n\u003chttps://leetcode.com/problems/binary-tree-postorder-traversal/\u003e\n\n\u003chttps://leetcode.com/problems/binary-tree-postorder-traversal/discuss/45803/java-solution-using-two-stacks\u003e\n\n\u003chttps://leetcode.com/problems/binary-tree-postorder-traversal/discuss/45551/preorder-inorder-and-postorder-iteratively-summarization\u003e\n\n\u003chttps://leetcode.com/problems/binary-tree-postorder-traversal/discuss/45621/preorder-inorder-and-postorder-traversal-iterative-java-solution\u003e\n\n \n\n[LeetCode All in One 题目讲解汇总(持续更新中...)](http://www.cnblogs.com/grandyang/p/4606334.html)\n","author":{"url":"https://github.com/grandyang","@type":"Person","name":"grandyang"},"datePublished":"2019-05-30T16:15:57.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/145/leetcode/issues/145"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:b1ceed32-b68d-1bab-bf18-dddc93c647e0
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8684:22A813:3396051:448682E:6A654F45
html-safe-nonce638df9704e6a86c87da44edc16a922dc79594ab729feca7a3240150504c0fa9d
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4Njg0OjIyQTgxMzozMzk2MDUxOjQ0ODY4MkU6NkE2NTRGNDUiLCJ2aXNpdG9yX2lkIjoiODk0MzM0MTgyNDUwMzAwOTA5MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac33cf8e5a806c00ae5a210ea1fed7aaec192b0c8c3ccf3efce52ce5cd2bdbf9eb
hovercard-subject-tagissue:450387150
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/grandyang/leetcode/145/issue_layout
twitter:imagehttps://opengraph.githubassets.com/2569ff0381e44c86f64e7ec3e15ce76fa2c76f39c2a6789caf60a0512ee3cbc1/grandyang/leetcode/issues/145
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/2569ff0381e44c86f64e7ec3e15ce76fa2c76f39c2a6789caf60a0512ee3cbc1/grandyang/leetcode/issues/145
og:image:altGiven a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it it...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamegrandyang
hostnamegithub.com
expected-hostnamegithub.com
None52c76df668885aaff23b50bdca1fa1ea44ac9c1553e888ebc70ff1e4daa4625b
turbo-cache-controlno-preview
go-importgithub.com/grandyang/leetcode git https://github.com/grandyang/leetcode.git
octolytics-dimension-user_id8553010
octolytics-dimension-user_logingrandyang
octolytics-dimension-repository_id189444423
octolytics-dimension-repository_nwograndyang/leetcode
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id189444423
octolytics-dimension-repository_network_root_nwograndyang/leetcode
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release309153364422b3c499922d1a2a6404910a58ed8e
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/grandyang/LeetCode-All-In-One/issues/145#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgrandyang%2Fleetcode%2Fissues%2F145
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
Code QualityEnforce quality at mergehttps://github.com/features/code-quality
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgrandyang%2Fleetcode%2Fissues%2F145
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=grandyang%2Fleetcode
Reloadhttps://github.com/grandyang/LeetCode-All-In-One/issues/145
Reloadhttps://github.com/grandyang/LeetCode-All-In-One/issues/145
Reloadhttps://github.com/grandyang/LeetCode-All-In-One/issues/145
grandyang https://github.com/grandyang
leetcodehttps://github.com/grandyang/leetcode
Please reload this pagehttps://github.com/grandyang/LeetCode-All-In-One/issues/145
Notifications https://github.com/login?return_to=%2Fgrandyang%2Fleetcode
Fork 734 https://github.com/login?return_to=%2Fgrandyang%2Fleetcode
Star 6.2k https://github.com/login?return_to=%2Fgrandyang%2Fleetcode
Code https://github.com/grandyang/leetcode
Issues 2k https://github.com/grandyang/leetcode/issues
Pull requests 1 https://github.com/grandyang/leetcode/pulls
Discussions https://github.com/grandyang/leetcode/discussions
Actions https://github.com/grandyang/leetcode/actions
Projects https://github.com/grandyang/leetcode/projects
Security and quality 0 https://github.com/grandyang/leetcode/security
Insights https://github.com/grandyang/leetcode/pulse
Code https://github.com/grandyang/leetcode
Issues https://github.com/grandyang/leetcode/issues
Pull requests https://github.com/grandyang/leetcode/pulls
Discussions https://github.com/grandyang/leetcode/discussions
Actions https://github.com/grandyang/leetcode/actions
Projects https://github.com/grandyang/leetcode/projects
Security and quality https://github.com/grandyang/leetcode/security
Insights https://github.com/grandyang/leetcode/pulse
[LeetCode] 145. Binary Tree Postorder Traversalhttps://github.com/grandyang/LeetCode-All-In-One/issues/145#top
https://github.com/grandyang
grandyanghttps://github.com/grandyang
on May 30, 2019https://github.com/grandyang/leetcode/issues/145#issue-450387150
Binary Tree Preorder Traversalhttp://www.cnblogs.com/grandyang/p/4146981.html
#145https://github.com/grandyang/leetcode/issues/145
Binary Tree Preorder Traversalhttp://www.cnblogs.com/grandyang/p/4146981.html
Binary Tree Inorder Traversalhttp://www.cnblogs.com/grandyang/p/4297300.html
Binary Tree Level Order Traversalhttp://www.cnblogs.com/grandyang/p/4051321.html
https://leetcode.com/problems/binary-tree-postorder-traversal/https://leetcode.com/problems/binary-tree-postorder-traversal/
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/45803/java-solution-using-two-stackshttps://leetcode.com/problems/binary-tree-postorder-traversal/discuss/45803/java-solution-using-two-stacks
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/45551/preorder-inorder-and-postorder-iteratively-summarizationhttps://leetcode.com/problems/binary-tree-postorder-traversal/discuss/45551/preorder-inorder-and-postorder-iteratively-summarization
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/45621/preorder-inorder-and-postorder-traversal-iterative-java-solutionhttps://leetcode.com/problems/binary-tree-postorder-traversal/discuss/45621/preorder-inorder-and-postorder-traversal-iterative-java-solution
LeetCode All in One 题目讲解汇总(持续更新中...)http://www.cnblogs.com/grandyang/p/4606334.html
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.