代码随想录算法训练营day14||二叉树的遍历

144. 二叉树的前序遍历

递归写法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List preorderTraversal(TreeNode root) {
        List re=new ArrayList();
        qinabian(root,re);
        return re;
    }
    public void qinabian(TreeNode root,List re)
    {
        if(root==null)
        {
            return;
        }
        re.add(root.val);
        qinabian(root.left,re);
        qinabian(root.right,re);
    }
}

非递归写法(迭代)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List preorderTraversal(TreeNode root) {
Stack  stak=new Stack();
List re=new ArrayList();
if(root==null)
{
    return re;
}
stak.push(root);//将根节点其放入栈
while(!stak.isEmpty())
{
        TreeNode node=stak.pop();
        re.add(node.val);//出栈
        if(node.right!=null)
        {
            stak.push(node.right);//右侧入栈
        }
        if(node.left!=null)
        {
            stak.push(node.left);//左侧入栈
        }
}
return re;
    }
}

94. 二叉树的中序遍历

递归写法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List inorderTraversal(TreeNode root) {
        List ladd=new ArrayList();
        middol(root,ladd);
        return ladd;
    }
    public void middol(TreeNode root,List ladd)
    {
        if(root==null)
        {
            return;
        }
       middol(root.left,ladd);
       ladd.add(root.val);
       middol(root.right,ladd);

    }
}

非递归写法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List inorderTraversal(TreeNode root) {
        Stack   sak=new Stack();
        List rel=new ArrayList();
        TreeNode node=root;
        while(node!=null||!sak.isEmpty())
        {
            if(node!=null)
            {
//左侧入栈
                sak.push(node);
                node=node.left;
            }
            else
            {
//右侧入栈
                node=sak.pop();
                rel.add(node.val);
                node=node.right;
            }
        }
        return rel;
        
            }
}

145. 二叉树的后序遍历

递归写法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List postorderTraversal(TreeNode root) {
        List jg=new ArrayList();
        lastbl(root,jg);
        return jg;
    }
    public void lastbl(TreeNode root,List jg)
    {
        if(root==null)
        {
            return;
        }
        lastbl(root.left,jg);
        lastbl(root.right,jg);
        jg.add(root.val);
    }
}

非递归写法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List postorderTraversal(TreeNode root) {
        List da=new ArrayList();
        Stack  sa=new Stack();
        if(root==null)
        {
            return da;
        }
//前序遍历迭代写法
        sa.push(root);
        while(!sa.isEmpty())
        {
             TreeNode nodd=sa.pop();
             da.add(nodd.val);
             if(nodd.left!=null)
             {
                 sa.push(nodd.left);
             }
             if(nodd.right!=null)
             {
                 sa.push(nodd.right);
             }

        } 
//翻转数组
        Collections.reverse(da);
        return da;
    }
}

 总结:迭代写法还需进一步理解,加强印象

你可能感兴趣的:(算法)