。二元樹後序遍歷
145。二元樹後序遍歷
難度:簡單
主題:堆疊、樹、深度優先搜尋、二元樹
給定二元樹的根,回傳其節點值的後序遍歷。
範例1:
- 輸入: root = [1,null,2,3]
- 輸出: [3,2,1]
範例2:
- 輸入: root = []
- 輸出: []
範例 3:
- 輸入: root = [1]
- 輸出: [1]
約束:
- 樹中節點的數量在 [0, 100] 範圍內。
- -100
解:
我們可以使用堆疊的迭代方法。後序遍歷遵循以下順序:左、右、根。
讓我們用 PHP 實作這個解:145。二元樹後序遍歷
<?php // Definition for a binary tree node. class TreeNode { public $val = null; public $left = null; public $right = null; public function __construct($val = 0, $left = null, $right = null) { $this->val = $val; $this->left = $left; $this->right = $right; } } /** * @param TreeNode $root * @return Integer[] */ function postorderTraversal($root) { ... ... ... /** * go to ./solution.php */ } // Example usage: // Example 1 $root1 = new TreeNode(1); $root1->right = new TreeNode(2); $root1->right->left = new TreeNode(3); print_r(postorderTraversal($root1)); // Output: [3, 2, 1] // Example 2 $root2 = null; print_r(postorderTraversal($root2)); // Output: [] // Example 3 $root3 = new TreeNode(1); print_r(postorderTraversal($root3)); // Output: [1] ?>
解釋:
TreeNode 類別: TreeNode 類別定義二元樹中的節點,包括其值、左子節點和右子節點。
-
postorder遍歷函數:
- 我們初始化一個空的結果陣列和一個堆疊。
- 我們使用 while 循環,只要堆疊不為空或目前節點不為空,循環就會繼續。
- 如果目前節點不為空,我們將其壓入堆疊並移至其左子節點。
- 如果目前節點為空,我們檢查棧頂節點。如果它有一個我們還沒有訪問過的右孩子,我們就會移動到右孩子。否則,我們將節點的值加到結果數組中並將其從堆疊中彈出。
這種迭代方法模擬了遞歸後序遍歷,而不使用系統遞歸,從而更加節省記憶體。
聯絡連結
如果您發現本系列有幫助,請考慮在 GitHub 上給 存儲庫 一個星號或在您最喜歡的社交網絡上分享該帖子? 。您的支持對我來說意義重大!
如果您想要更多類似的有用內容,請隨時關注我:
- 領英
- GitHub
以上是。二元樹後序遍歷的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

避免N 1查詢問題,通過提前加載關聯數據來減少數據庫查詢次數;2.僅選擇所需字段,避免加載完整實體以節省內存和帶寬;3.合理使用緩存策略,如Doctrine的二級緩存或Redis緩存高頻查詢結果;4.優化實體生命週期,定期調用clear()釋放內存以防止內存溢出;5.確保數據庫索引存在並分析生成的SQL語句以避免低效查詢;6.在無需跟踪變更的場景下禁用自動變更跟踪,改用數組或輕量模式提升性能。正確使用ORM需結合SQL監控、緩存、批量處理和適當優化,在保持開發效率的同時確保應用性能。

ReadonlypropertiesinPHP8.2canonlybeassignedonceintheconstructororatdeclarationandcannotbemodifiedafterward,enforcingimmutabilityatthelanguagelevel.2.Toachievedeepimmutability,wrapmutabletypeslikearraysinArrayObjectorusecustomimmutablecollectionssucha

bcmathisesene forAccratecryptoCurrencyCalcalsionSinphpBecausefloing-pointarithmeticIntroducesunAcceptablebablerOundingErrors.1.floation-pointnumberslike0.1 0.2yieldimimpreciseresults(e.g.,e.g.,0.30000000000000000000004)

PhpeValuatesConstantExpressatAtcompiletimetoetimetoemetotocreveranceandearlyerrordetection.1.ConstantExpressepressevaluationMeanScomputingValuesDuruesduresduresduring-CompiLation -whenalloperandSareSareSareConconstantSareConconstantsLikeLiterals,classConstants,classConstants,classConstants,orpredefendinedconcontantstants.2.phpp'2.php’2.php’2.2.php’2.php’2.php’2.php’2.php’2.php’sse

Rawstringsindomain-drivenapplicationsshouldbereplacedwithvalueobjectstopreventbugsandimprovetypesafety;1.Usingrawstringsleadstoprimitiveobsession,whereinterchangeablestringtypescancausesubtlebugslikeargumentswapping;2.ValueobjectssuchasEmailAddressen

使用guazzleforbusthttprequestswithheadersand andtimeouts.2.parsehtmleffitedlywithsymfonydomcrawlerusingcssselectors.3.handlejavascript-heavysitesby-heavysitesbyintegrationpuppeepetementegratingpuppeeteviaphpage()

浮點數不精確是PHP中常見問題,答案在於其使用IEEE754雙精度格式導致十進制小數無法精確表示;1.0.1或0.2等數在二進制中為無限循環小數,計算機需截斷造成誤差;2.比較浮點數時應使用容差而非==,如abs($a-$b)

Switchcanbeslightlyfasterthanif-elsewhencomparingasinglevariableagainstmultiplescalarvalues,especiallywithmanycasesorcontiguousintegersduetopossiblejumptableoptimization;2.If-elseisevaluatedsequentiallyandbettersuitedforcomplexconditionsinvolvingdiff
