Open Shawngbk opened 7 years ago
leaf的意思是没有left or right子节点
/**
public class Solution { public int sumOfLeftLeaves(TreeNode root) { if(root == null) return 0; if(root.left != null && isLeaf(root.left)) return root.left.val + sumOfLeftLeaves(root.right); else return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right); } private boolean isLeaf(TreeNode root) { if(root.left == null && root.right == null) return true; else return false; } }
Facebook
leaf的意思是没有left or right子节点
/**
public class Solution { public int sumOfLeftLeaves(TreeNode root) { if(root == null) return 0; if(root.left != null && isLeaf(root.left)) return root.left.val + sumOfLeftLeaves(root.right); else return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right); } private boolean isLeaf(TreeNode root) { if(root.left == null && root.right == null) return true; else return false; } }