剑指offer(1/10)

层次遍历

从上往下打印出二叉树的每个节点,同层节点从左至右打印。

import java.util.ArrayList;
import java.util.LinkedList;
public class Solution {
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        if(root == null) return list;
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            list.add(queue.pop().val);
            if(root.left != null)
                queue.add(root.left);
            if(root.right != null)
                queue.add(root.right);
            if(!queue.isEmpty())
                root=queue.peek();
        }
        return list;
    }
}
class Solution {
public:
    vector<int> PrintFromTopToBottom(TreeNode* root) {
        vector<int>  que;
        queue<TreeNode*>q;
        TreeNode* fr;
        if(root == NULL) return que;
        q.push(root);
        while(!q.empty())
        {
        fr=q.front();
        que.push_back(fr->val);
        if(fr->left != NULL)
            q.push(fr->left);
        if(fr->right != NULL)
            q.push(fr->right);
        q.pop();
        }
       return que;
    }
};

二叉搜索树的后序遍历

输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则返回true,否则返回false。假设输入的数组的任意两个数字都互不相同。

public class Solution {
   public static boolean VerifySquenceOfBST(int [] sequence) {
        /*
        二叉搜索树左边的树比根节点树小;
        右边大的树比根节点大
        后序遍历最后一个元素必为根节点。
         */
        if (sequence == null || sequence.length == 0) {
            return false;
        }
        return dg(sequence,0,sequence.length-1);

    }
    public static boolean dg(int [] sequence,int start,int end){
       if (start >= end) {
           return true;
       }
       int i = 0;
        int root = sequence[end];
        for (i = start; i < end; i++) {
            if (sequence[i]>root){
                break;
            }
        }
        for (int j = i; j <= end-1 ; j++) {
            if (sequence[j] < root){
                return false;
            }
        }
        return dg(sequence,start,i-1) && dg(sequence,i,end-1);
    }
}
class Solution {
public:
    bool VerifySquenceOfBST(vector<int> sequence) {
        int size = sequence.size();
        if(0==size)return false;
        int i = 0;
        while(--size)
        {
            while(sequence[i++]<sequence[size]);
            while(sequence[i++]>sequence[size]);
            if(i<size)return false;
            i=0;
        }
        return true;
    }
};

二叉树的和为某一值的路径

输入一颗二叉树的根节点和一个整数,按字典序打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。

import java.util.ArrayList;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    private ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
        dfs(root,target,new ArrayList<>());
        return ret;
    }
    public void dfs(TreeNode root,int target,ArrayList<Integer> list){
        if(root == null)
            return;
        list.add(root.val);
        target -= root.val;
        if(target == 0 && root.left == null && root.right == null)
            ret.add(new ArrayList<>(list));
        else{
            dfs(root.left,target,list);
            dfs(root.right,target,list);
        }
        list.remove(list.size() -1);
    }
}
/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    vector<vector<int> > ret;
    vector<int> temp;
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        //dfs思路
        if(!root) return ret;
        temp.push_back(root->val);
        if(!root->left&&!root->right&&expectNumber==root->val)
            ret.push_back(temp);
        FindPath(root->left, expectNumber-root->val);
        FindPath(root->right, expectNumber-root->val);
        temp.pop_back();
        return ret;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值