1. 程式人生 > 實用技巧 >二叉樹後續遍歷序列-遞迴

二叉樹後續遍歷序列-遞迴

想法都對了但是沒寫明白,不能把left和right分開看

//最後一個節點是根節點,從開始找第一個大於根的是右子樹起始,
//劃分左右子樹遞迴進行判斷,如果左有大於根的或者右有小於根的,false
class Solution {
    public boolean verifyPostorder(int[] postorder) {
        if(postorder==null||postorder.length<=1){
            return true;
        }
        int len=postorder.length;
        return
helper(postorder,0,len-1); } public boolean helper(int []postorder,int start,int end){ if(start>=end){ return true; } int root=postorder[end]; int i=start; while(postorder[i]<root)i++; int mid=i; for(int
j=mid;j<end;j++){ if(postorder[j]<root){ return false; } } return helper(postorder,start,mid-1)&&helper(postorder,mid,end-1); } }