leetcode刷题之树

树的三种遍历方式 的 非递归版本
中序遍历
下面的解法就是相当于 第一趟先把元素按照中序的顺序进栈
第二趟 是相当于把 null标记过的位置给拿出来

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
                ArrayList<Integer> res = new ArrayList<>();
                LinkedList <TreeNode>qu = new LinkedList<>();
                if(root!=null)
                    qu.addLast(root);
                while(!qu.isEmpty())
                {
                    if(qu.peekLast()!=null)
                    {
                        TreeNode tmp = qu.pollLast();
               // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
                        if(tmp.right!=null) qu.addLast(tmp.right);
                      //先添加右节点 因为栈是先进后出
                        qu.addLast(tmp);
                      //添加中节点  
                        qu.addLast(null);
                        //中节点访问过了  但是好没有处理做一下标记
                        if(tmp.left!=null) qu.addLast(tmp.left);
                        //添加左节点
                    }
                    else{
                        qu.pollLast();
                        //去null
                        TreeNode tmp = qu.pollLast();
						//取出栈中元素
                        res.add(tmp.val);
                        //放入到结果中去
                    }
                }
                return res;
            }
    
}

先序遍历

class Solution {
        public List<Integer> preorderTraversal(TreeNode root) {
                 ArrayList<Integer> res =  new ArrayList<>();
        LinkedList <TreeNode> st = new LinkedList();
        if(root!=null)
            st.addLast(root);
        while(!st.isEmpty())
        {
            if(st.peekLast()!=null)
            {
            //这个地方注意了想先让那个先出来 就把他放到最后
                TreeNode tmp = st.pollLast();
                if(tmp.right!=null)   //右
                    st.addLast(tmp.right);
                if(tmp.left!=null)  //左
                    st.addLast(tmp.left);
                st.addLast(tmp); //中
                st.addLast(null);
            }
            else{
                st.removeLast();
                TreeNode  tmp = st.removeLast();
                res.add(tmp.val);

            }
        }
    return res;
    }

        
}

公共父节点

public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    if(root==null || root==p || root==q) //该节点是否存在p或者q
        return root;
    
    TreeNode leftNode=lowestCommonAncestor(root.left,p,q);
    TreeNode rightNode=lowestCommonAncestor(root.right,p,q);

    if(leftNode==null) //同在右侧
        return rightNode;
    if(rightNode==null) //同在左侧
        return leftNode;

    return root; //如果是在两边就返回 该节点
}

左叶子节点之和
计算给定二叉树的所有左叶子之和。

  • 思考什么是左叶子节点
root.left!=null&&root.left.left==null&&root.left.right==null

满足 左右子树都为null 并且 是某个树的子节点

  • 然后就简单了 递归 结束条件就是 root ==null
  • 然后向左右子树不断递归
class Solution {
    int i =0;
    public int sumOfLeftLeaves(TreeNode root) {
        dfs(root);
        return i;
    }
    public void dfs(TreeNode root)
    {
        if(root==null)
        return;
        if(root.left!=null&&root.left.left==null&&root.left.right==null)
        i =i+root.left.val;
        sumOfLeftLeaves(root.left);
        sumOfLeftLeaves(root.right);
    }
}

简洁一点的写法

class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if(root ==null)
        return 0;
        int sum =0;//利用sum做累加  
        if(root.left!=null&&root.left.left==null&&root.left.right==null)
        sum =root.left.val;
        return sum + sumOfLeftLeaves(root.left)+sumOfLeftLeaves(root.right);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值