112. Path Sum(深搜、遞歸)

package Recursion;


public class HasPathSum_112 {
	public class TreeNode {
		int val;
		TreeNode left;
		TreeNode right;

		TreeNode(int x) {
			val = x;
		}
	}
	public boolean hasPathSum(TreeNode root, int sum) {
		if(root==null) {
			return false;
		}
		if(root.left==null&&root.right==null) {
			return sum-root.val==0;
		}
		return hasPathSum(root.left, sum-root.val)||hasPathSum(root.right, sum-root.val);
	}

}

發佈了93 篇原創文章 · 獲贊 8 · 訪問量 9058
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章