zoukankan      html  css  js  c++  java
  • 前序遍历(递归、非递归)、层序遍历(递归、非递归)

    递归:

     1 package 剑指offer.前序遍历;
     2 
     3 import java.util.ArrayList;
     4 
     5 /**
     6  * Created by nick on 2018/10/6.
     7  */
     8 public class Solution {
     9     ArrayList<Integer> res=new ArrayList<Integer>();
    10     public void preNode(TreeNode root){
    11         if(root==null)
    12             return;
    13         res.add(root.val);
    14         preNode(root.left);
    15         preNode(root.right);
    16     }
    17     
    18 }
    19 class TreeNode {
    20     int val = 0;
    21     TreeNode left = null;
    22     TreeNode right = null;
    23 
    24     public TreeNode(int val) {
    25         this.val = val;
    26 
    27     }
    28 }

    非递归:

     1 package 剑指offer.前序遍历;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Stack;
     5 
     6 /**
     7  * Created by nick on 2018/10/6.
     8  */
     9 public class Solution1 {
    10     ArrayList<Integer> res=new ArrayList<Integer>();
    11     public ArrayList<Integer> preNode(TreeNode root){
    12         Stack<TreeNode> temp=new Stack<TreeNode>();
    13         if(root==null)
    14             return res;
    15         temp.push(root);
    16         while (!temp.isEmpty()){
    17             TreeNode t=temp.pop();
    18             res.add(t.val);
    19             if(t.right!=null)
    20                 temp.push(root.right);
    21             if(t.left!=null)
    22                 temp.push(root.left);
    23 
    24         }
    25         return res;
    26     }
    27 
    28     public static void main(String[] args) {
    29         TreeNode root=new TreeNode(1);
    30         root.left=new TreeNode(2);
    31         root.right=new TreeNode(3);
    32 
    33         System.out.println(new Solution1().preNode(root));
    34     }
    35 }

     层序遍历:

        /*
         * 层序遍历
         * 递归
         */
        public void levelOrder(BinaryNode<AnyType> Node) {
            if (Node == null) {
                return;
            }
    
            int depth = depth(Node);
    
            for (int i = 1; i <= depth; i++) {
                levelOrder(Node, i);
            }
        }
    
        private void levelOrder(BinaryNode<AnyType> Node, int level) {
            if (Node == null || level < 1) {
                return;
            }
    
            if (level == 1) {
                System.out.print(Node.element + "  ");
                return;
            }
    
            // 左子树
            levelOrder(Node.left, level - 1);//直到第level层,再进行打印
    
            // 右子树
            levelOrder(Node.right, level - 1);
        }
    
        public int depth(BinaryNode<AnyType> Node) {
            if (Node == null) {
                return 0;
            }
    
            int l = depth(Node.left);
            int r = depth(Node.right);
            if (l > r) {
                return l + 1;
            } else {
                return r + 1;
            }
        }

    非递归算法:

        //使用队列
        public static void levelTraversal2(TreeNode root)
        {
            ArrayDeque<TreeNode> queue = new ArrayDeque<>();
            queue.add(root);
            while(!queue.isEmpty())
            {
                TreeNode temp = queue.poll();
                System.out.print(temp.val);
                if(temp.left != null)
                {
                    queue.offer(temp.left);
                }
                if(temp.right != null)
                {
                    queue.offer(temp.right);
                }
            }
        }
    }
  • 相关阅读:
    Binary Search Tree Iterator 解答
    Invert Binary Tree 解答
    Min Stack 解答
    Trapping Raining Water 解答
    Candy 解答
    Jump Game II 解答
    Implement Hash Map Using Primitive Types
    Gas Station 解答
    Bucket Sort
    HashMap 专题
  • 原文地址:https://www.cnblogs.com/nickup/p/9746993.html
Copyright © 2011-2022 走看看